【发布时间】:2018-09-23 06:29:23
【问题描述】:
我无法让我的 tableView 加载来自 Firebase Firestore 的数据。我在 generateMore() 函数中遍历 cmets 文档,并将添加注释作为 AttributedTextComment 分配给数组。但是当我在视图控制器的viewDidLoad() 中设置数组时,数组保持为空,我不知道为什么。谢谢你的帮助!此外,我正在使用可以在 Github 上找到的 SwiftyComments 库,如果这有助于理解代码。
编辑:generateMore() 函数中的数组按预期填充了所有 Firestore 数据,但由于某种原因,ViewController 中的allcomments 永远不会设置为等于该数组。
class RandomDiscussion {
var comments: [AttributedTextComment]! = []
var colRef: CollectionReference!
func generateMore() -> [AttributedTextComment] {
var arr: [AttributedTextComment]! = []
colRef = Firestore.firestore().collection("pictures/TKIiXdontufmDM1idbVH/comments")
let query = colRef.whereField("body", isGreaterThan: "")
query.getDocuments() { (querySnapshot, err) in
if err != nil {
print("error")
return
}
else {
for doc in querySnapshot!.documents {
print("\(doc.documentID) => \(doc.data())")
let com = AttributedTextComment()
com.posterName = doc.get("username") as? String
com.body = doc.get("body") as? String
com.upvotes = doc.get("upvotes") as? Int
com.downvotes = doc.get("downvotes") as? Int
arr.append(com)
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "load"), object: nil)
}
}
}
return arr
}
}
class RedditCommentsViewController: CommentsViewController {
private let commentCellId = "redditComentCellId"
var allComments: [AttributedTextComment] = []
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(RedditCommentCell.self, forCellReuseIdentifier: commentCellId)
tableView.backgroundColor = RedditConstants.backgroundColor
NotificationCenter.default.addObserver(self, selector: #selector(loadList), name: NSNotification.Name(rawValue: "load"), object: nil)
allComments = RandomDiscussion().generateMore()
currentlyDisplayed = allComments
self.swipeToHide = true
self.swipeActionAppearance.swipeActionColor = RedditConstants.flashyColor
}
override open func commentsView(_ tableView: UITableView, commentCellForModel commentModel: AbstractComment, atIndexPath indexPath: IndexPath) -> CommentCell {
let commentCell = tableView.dequeueReusableCell(withIdentifier: commentCellId, for: indexPath) as! RedditCommentCell
let comment = currentlyDisplayed[indexPath.row] as! RichComment
commentCell.level = comment.level
commentCell.commentContent = comment.body
commentCell.posterName = comment.posterName
//commentCell.date = comment.soMuchTimeAgo()
commentCell.upvotes = comment.upvotes
commentCell.isFolded = comment.isFolded && !isCellExpanded(indexPath: indexPath)
return commentCell
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.barTintColor = RedditConstants.flashyColor
self.navigationController?.navigationBar.tintColor = .white
UIApplication.shared.statusBarStyle = .lightContent
}
@objc func loadList(){
self.tableView.reloadData()
}
}
【问题讨论】:
-
我们可以看看你的“loadList”函数吗?
-
问题可能出在 whereField("body", isGreaterThan: "") 。 isgreaterThan 约束按字母顺序排列
-
@Oliver 刚刚更新了它。它只是试图重新加载 tableView
标签: ios swift uitableview firebase google-cloud-firestore