[英]How to fetch data from Cloud Firestore to Xcode Uikit TableViewController?
[英]Add subviews from Firestore data (UIKit)
我遇到无法将多个子视图添加到 UIView 的问题。运行我的代码时,我成功地能够看到来自 Firestore 的数据。 我也能够在我的 canvasView 上看到一个子视图,但之后没有其他任何东西出现。
func fetchObjects() {
let index = IndexPath(row: 1, section: 0)
let cell: BookCell = self.backgroundTable.cellForRow(at: index) as! BookCell
let newButton = cell.bookButton
guard let userID = Auth.auth().currentUser?.uid else { return }
COLLECTION_BOOKS.document(userID).collection("collections").getDocuments { (snapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
for document in snapshot!.documents {
let imageURL = document.get("imageURL") as! String
let xLocation = document.get("xLocation") as! Double
let yLocation = document.get("yLocation") as! Double
let buttonPhotoString = imageURL
if let url = URL(string: buttonPhotoString) {
let data = try? Data(contentsOf: url)
if let imageData = data {
guard let image = UIImage(data: imageData) else { return }
newButton.setImage(image, for: .normal)
newButton.frame.origin.x = xLocation
newButton.frame.origin.y = yLocation
cell.canvasView.addSubview(newButton)
} else {
print("Unable to get image data")
}
} else {
print("Unable to process url string")
}
}
}
}
}
更新了答案。 我删除了引用单元格按钮的常量,而是在循环中创建了一个新按钮。
func fetchObjects() {
let index = IndexPath(row: 1, section: 0)
let cell: BookCell = self.backgroundTable.cellForRow(at: index) as! BookCell
guard let userID = Auth.auth().currentUser?.uid else { return }
COLLECTION_BOOKS.document(userID).collection("collections").getDocuments { (snapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
for document in snapshot!.documents {
var new = UIButton()
let imageURL = document.get("imageURL") as! String
let xLocation = document.get("xLocation") as! Double
let yLocation = document.get("yLocation") as! Double
let buttonPhotoString = imageURL
if let url = URL(string: buttonPhotoString) {
let data = try? Data(contentsOf: url)
if let imageData = data {
guard let image = UIImage(data: imageData) else { return }
new = UIButton(frame: CGRect(x: 170, y: 142, width: 70, height: 70))
new.setImage(image, for: .normal)
new.frame.origin.x = xLocation
new.frame.origin.y = yLocation
cell.canvasView.addSubview(new)
} else {
print("Unable to get image data")
}
} else {
print("Unable to process url string")
}
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.