使用firebase数据库填充tableView

时间:2016-07-15 10:12:48

标签: ios uitableview firebase swift2 firebase-realtime-database

我正在尝试使用firebase数据库填充我的tableView。

以下是代码: -

import UIKit
import Firebase

class FriendsListViewController: UIViewController , UITableViewDataSource, UITableViewDelegate{

@IBOutlet weak var friendsListTableView: UITableView!

let ref = FIRDatabase.database().reference()
var FIRControllerClassHandle : FIRControllerClass = FIRControllerClass()
var imageCell  = [UIImage]()
var username  = [String]()
var userDesc  = [String]()

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    friendsListTableView.alpha = 0
    friendsListTableView.delegate = self
    friendsListTableView.dataSource = self

    populateTable({
        self.friendsListTableView.reloadData()
    })    
}

func populateTable(completionBlock : (() -> Void)){

    FIRControllerClassHandle.retrieveFriendListDatabase { (userIdA) in

        for a in 1 ... userIdA.count-1 {
            repeat {

                self.FIRControllerClassHandle.retrieveStorageForFriendListCell(userIdA[a] as! String, completion: { (image) in
                    print("image transferred in the friendlist block : \(image)")
                    print("user id in friendList : \(userIdA[a])")

                    self.imageCell.append(image) 
                })

                self.FIRControllerClassHandle.retrieveDatabaseForFriendListCell(userIdA[a] as! String, completion: { (profile) in


                    self.username.append((profile["username"] as? String)!)
                    self.userDesc.append((profile["briefDecription"] as? String)!)
                    completionBlock()   
                })
            } while(a <= userIdA.count-1)
        }
       }
    }

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    print("username count in the no of rows : \(username.count)")

    return username.count ?? 0
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    print("username in cellForIndexpath : \(self.username)")

    let cell = friendsListTableView.dequeueReusableCellWithIdentifier("friendListCell") as! FriendsListTableViewCell

    cell.friendListProfileName.text = username[indexPath.row]
    cell.friendListProfileDescription.text = userDesc[indexPath.row]
    cell.friendListProfilePicture.image = imageCell[indexPath.row]

    return cell
}

@IBAction func backBtnAction(sender: UIButton) {

    let homePageScene = self.navigationController?.storyboard?.instantiateViewControllerWithIdentifier("HomePageFeedViewControllerVC_ID") as! HomePageFeedViewController
    self.navigationController?.pushViewController(homePageScene, animated: true)

}


}

这是一个无限循环, userIdA是我存储了所有user.uid的数组 self.FIRControllerClassHandle.retrieveStorageForFriendListCell是单独的FIRController类中的函数,它返回用户的个人资料图片 类似FIRControllerClassHandle.retrieveDatabaseForFriendListCell用于检索数据库 我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

你有两个嵌套在一起的循环:

   for a in 1 ... userIdA.count-1 {
        repeat{
            // ...
        }while(a <= userIdA.count-1)
    }

在内循环中,a不会改变,因此它将永远循环。我猜你只想要另一个循环。