[英]Reload data in tableview Firebase with Swift 3
我遵循了制作Twitter克隆的视频教程。 我猜这个教程写得很快。 我试图申请Swift3。但是在第3个视频中,我遇到了问题。 我可以保存推文,但不知道如何在表格视图中显示。 他正在使用以下行:
let tweet = tweets[(self.tweets.count-1) - indexPath.row]!.value["text"] as! String
视频系列: 带有Firebase的Twitter克隆
项目在这里: Github链接
我的问题在这里:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: HomeViewTableViewCell = tableView.dequeueReusableCellWithIdentifier("HomeViewTableViewCell", forIndexPath: indexPath) as! HomeViewTableViewCell
let tweet = tweets[(self.tweets.count-1) - indexPath.row]!.value["text"] as! String
cell.configure(nil,name:self.loggedInUserData!.value["name"] as! String,handle:self.loggedInUserData!.value["handle"] as! String,tweet:tweet)
return cell
}
错误:“对成员计数的引用不明确”。
因此,为了显示推文,您需要向Firebase实时数据库添加观察者。
self.databaseRef.child("user_profiles").child(self.loggedInUser!.uid).observeSingleEventOfType(.Value) { (snapshot:FIRDataSnapshot) in
//store the logged in users details into the variable
self.loggedInUserData = snapshot
print(self.loggedInUserData)
//get all the tweets that are made by the user
self.databaseRef.child("tweets/\(self.loggedInUser!.uid)").observeEventType(.ChildAdded, withBlock: { (snapshot:FIRDataSnapshot) in
self.tweets.append(snapshot)
self.homeTableView.insertRowsAtIndexPaths([NSIndexPath(forRow:0,inSection:0)], withRowAnimation: UITableViewRowAnimation.Automatic)
self.aivLoading.stopAnimating()
}){(error) in
print(error.localizedDescription)
}
}
如果您查看以下部分,
self.databaseRef.child("tweets/\(self.loggedInUser!.uid)").observeEventType(.ChildAdded, withBlock: { (snapshot:FIRDataSnapshot) in
self.tweets.append(snapshot)
self.homeTableView.insertRowsAtIndexPaths([NSIndexPath(forRow:0,inSection:0)], withRowAnimation: UITableViewRowAnimation.Automatic)
self.aivLoading.stopAnimating()
}){(error) in
print(error.localizedDescription)
}
他正在向数据库添加观察者,并且只要将节点添加到引用中,就会触发上述代码。 检查您是否正确完成了此操作。
如果正确完成此操作,请确保已将观察者添加到精确节点。 在这里,观察者附加到名为tweet
> userID
的节点上。 如果您具有不同的数据库配置,则您的引用将有所不同。
在您学习的过程中,我会留给您进行Swift 3语法的转换。
最后,我解决了我的问题。 像这样为Swift 3.1更新:
self.homeTableView.insertRows(at: [IndexPath(row:self.tweets.count-1,section:0)], with: UITableViewRowAnimation.automatic)
我在viewDidLoad中使用了上面的代码。 我使用tweets [indexPath.row]。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.