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

时间:2017-06-13 13:18:32

标签: ios uitableview swift3

我按照视频教程制作推特克隆。本教程是用swift 2编写的。我试图申请Swift 3.但是在第3段视频中我遇到了问题。我可以保存推文,但我不知道如何在tableview中显示它。他正在使用这一行:

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
}

错误:“对成员计数的模糊引用”。

2 个答案:

答案 0 :(得分: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语法。

答案 1 :(得分:0)

最后我解决了我的问题。对于Swift 3.1更新如下:

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

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

相关问题