簡體   English   中英

使用Swift 3在Tableview Firebase中重新加載數據

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM