【问题标题】:TableView not printing any cells (swift)TableView 不打印任何单元格(swift)
【发布时间】:2017-05-07 18:26:45
【问题描述】:

我目前正在尝试仅显示从我的 firebase 数据库中提取的简单值到 TableView 中。我得到的问题是单元格当前没有填充任何内容。我已经输入了打印语句,只是为了确保数组不为空,而事实并非如此。我正在使用以下代码:

    class DisplayTransactions: UITableViewController {

    var ref = FIRDatabase.database().reference()

    //gets current firebase users
    var userC = FIRAuth.auth()?.currentUser
    var pubCount: Int!
    var anArray = [String] ()

    override func viewDidLoad() {
        super.viewDidLoad()
        var transRef = FIRDatabase.database().reference().child("Users").child((userC?.uid)!).child("PaymentTransactions")

        //for Deposit children
        transRef.child("Deposit").observeSingleEventOfType(.Value, withBlock: { (snapshot) in
            //creates enumerator of the snapshot children for
            let enumerator = snapshot.children
            while let rest = enumerator.nextObject() as? FIRDataSnapshot {
                self.anArray.append(String(rest.value))

                //printing the rest.value to make sure there are children to iterate through
                print(rest.value)
            }
        })
        //for Withdrawl children
        transRef.child("Withdraw").observeSingleEventOfType(.Value, withBlock: { (snapshot) in
            //creates enumerator of the snapshot children for
            let enumerator = snapshot.children
            while let rest = enumerator.nextObject() as? FIRDataSnapshot {
                self.anArray.append(String(rest.value))

                //printing the rest.value to make sure there are children to iterate through
                print(rest.value)
            }
        })
        print(anArray)
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return anArray.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .Subtitle, reuseIdentifier: "cellID")

        cell.textLabel?.text = anArray[indexPath.row]
        print(anArray[indexPath.row])

        return cell
    }
}

【问题讨论】:

    标签: swift firebase firebase-realtime-database firebase-authentication


    【解决方案1】:

    填满数据源后,您似乎没有重新加载 UITableView。 每次更改数组的内容时,您都需要这样做。

    override func viewDidLoad() {
        super.viewDidLoad()
        var transRef = FIRDatabase.database().reference().child("Users").child((userC?.uid)!).child("PaymentTransactions")
    
        //for Deposit children
        transRef.child("Deposit").observeSingleEventOfType(.Value, withBlock: { (snapshot) in
            //creates enumerator of the snapshot children for
            let enumerator = snapshot.children
            while let rest = enumerator.nextObject() as? FIRDataSnapshot {
                self.anArray.append(String(rest.value))
    
                //printing the rest.value to make sure there are children to iterate through
                print(rest.value)
            }
            self.tableView.reloadData() //<<<<<<<<<<<< RELOAD TABLEVIEW
        })
        //for Withdrawl children
        transRef.child("Withdraw").observeSingleEventOfType(.Value, withBlock: { (snapshot) in
            //creates enumerator of the snapshot children for
            let enumerator = snapshot.children
            while let rest = enumerator.nextObject() as? FIRDataSnapshot {
                self.anArray.append(String(rest.value))
    
                //printing the rest.value to make sure there are children to iterate through
                print(rest.value)
            }
            self.tableView.reloadData() //<<<<<<<<<<<< RELOAD TABLEVIEW
        })
        print(anArray)
    }
    

    【讨论】:

    • 我试过这个,但没有任何改变。仍然显示空白单元格一秒钟,然后转到一个完全空白的页面。
    • 啊我的错,我原来放错了 .reloadData()
    • 是的,你需要把它们放在“块”里面而不是外面。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-20
    • 2017-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-30
    • 1970-01-01
    相关资源
    最近更新 更多