简体   繁体   English

使用Swift 3在Tableview Firebase中重新加载数据

[英]Reload data in tableview Firebase with Swift 3

I follow a video tutorial for making twitter clone. 我遵循了制作Twitter克隆的视频教程。 This tutorial is written with swift 2 I guess. 我猜这个教程写得很快。 I tried to apply for Swift 3. But in 3th video I have a problem. 我试图申请Swift3。但是在第3个视频中,我遇到了问题。 I can save tweets but I don't know how it can be shown in tableview. 我可以保存推文,但不知道如何在表格视图中显示。 He is using this line: 他正在使用以下行:

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

Video Series: twitter clone with firebase 视频系列: 带有Firebase的Twitter克隆

Project here: Github Link 项目在这里: Github链接

My problem here: 我的问题在这里:

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
}

Error: "Ambiguous reference to member count". 错误:“对成员计数的引用不明确”。

So in order to show the tweet you need to add a observer to your Firebase Real-Time Database. 因此,为了显示推文,您需要向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)
        }

    }

If you look at the following section, 如果您查看以下部分,

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)
    }

He is adding an observer to Database and anytime there is node getting added to the reference, the above code will be triggered. 他正在向数据库添加观察者,并且只要将节点添加到引用中,就会触发上述代码。 Check if you've done this correctly. 检查您是否正确完成了此操作。

In case you've done this correctly, make sure you've added the observer to exact node. 如果正确完成此操作,请确保已将观察者添加到精确节点。 Here, the observer is attached to a node called tweet --> userID . 在这里,观察者附加到名为tweet > userID的节点上。 If you have different configuration of database, your reference will be different. 如果您具有不同的数据库配置,则您的引用将有所不同。

As you are learning, I would leave the conversion to Swift 3 syntax on you. 在您学习的过程中,我会留给您进行Swift 3语法的转换。

Finally I solved my problem. 最后,我解决了我的问题。 Update like this for Swift 3.1: 像这样为Swift 3.1更新:

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

I used above code in viewDidLoad. 我在viewDidLoad中使用了上面的代码。 And I use tweets[indexPath.row]. 我使用tweets [indexPath.row]。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM