2017-06-13 56 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

相關問題