用firebase数据库填充tableView
问题描述:
我想填充我的tableView与firebase数据库。用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
用于检索数据库 我将如何解决这个问题?
答
你必须互相嵌套在两个循环:
for a in 1 ... userIdA.count-1 {
repeat{
// ...
}while(a <= userIdA.count-1)
}
在内部循环,a
不会改变,因此,它会永远循环下去。我想你只是想要另一个循环。
为什么'对于1 ...而不是'0'? – Droppy
因为我必须在后端初始化数组,所以它需要一个值来初始化它自己...所以在0索引它们是一个虚拟值实际userIdA数组从1开始。 – Dravidian
请尽量避免使用数组 - 将得到你陷入困境。整个过程可能会大大简化,但没有看到您的Firebase结构,很难说。你能不能将文章的片段作为文本发布(没有图片),我们来看看。 – Jay