【问题标题】:Populating tableView with firebase database使用 firebase 数据库填充 tableView
【发布时间】:2016-07-15 10:12:48
【问题描述】:

我正在尝试使用 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 ... 而不是0
  • 因为我必须在后端初始化数组并且它需要一个值来初始化自身......所以在 0 索引处它们是一个虚拟值实际 userIdA 数组从 1 开始。
  • 请尽量避免使用数组 - 这会给您带来麻烦。整个过程可能会大大简化,但如果没有看到您的 Firebase 结构,就很难分辨。你能把它的sn-p作为文本发布(没有图片),我们会看看。

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


【解决方案1】:

你有两个相互嵌套的循环:

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

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

【讨论】:

    猜你喜欢
    • 2018-11-22
    • 2020-06-06
    • 2016-11-09
    • 1970-01-01
    • 2021-04-27
    • 1970-01-01
    • 2018-09-25
    • 2013-09-27
    • 1970-01-01
    相关资源
    最近更新 更多