2016-07-15 84 views
0

我想填充我的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用於檢索數據庫 我將如何解決這個問題?

+0

爲什麼'對於1 ...而不是'0'? – Droppy

+0

因爲我必須在後端初始化數組,所以它需要一個值來初始化它自己...所以在0索引它們是一個虛擬值實際userIdA數組從1開始。 – Dravidian

+0

請儘量避免使用數組 - 將得到你陷入困境。整個過程可能會大大簡化,但沒有看到您的Firebase結構,很難說。你能不能將文章的片段作爲文本發佈(沒有圖片),我們來看看。 – Jay

回答

0

你必須互相嵌套在兩個循環:

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

在內部循環,a不會改變,因此,它會永遠循環下去。我想你只是想要另一個循環。