2017-06-13 59 views
0

我遵循制作twitter克隆的视频教程。我猜这个教程是用swift 2编写的。我试图申请Swift 3.但在第三部影片中,我有一个问题。我可以保存推文,但我不知道如何在tableview中显示它。他是用这条线:在tableview中重新加载数据Firebase与Swift 3

let tweet = tweets[(self.tweets.count-1) - indexPath.row]!.value["text"] as! String 

视频系列:twitter clone with firebase

项目位置:Github Link

我在这里的问题:

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 
} 

错误: “不明确提及成员计数”。

+0

您需要显示错误 –

回答

0

最后我解决了我的问题。更新这样的雨燕3.1:

self.homeTableView.insertRows(at: [IndexPath(row:self.tweets.count-1,section:0)], with: UITableViewRowAnimation.automatic) 

我在viewDidLoad中使用上面的代码。 我使用推文[indexPath.row]。

0

因此,为了显示推文,您需要将观察者添加到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语法。

+0

谢谢,但我的问题不在这里。我更新了问题。 – Mayday

相关问题