2017-07-18 43 views
1

我有一個UIColelctionView單元格應包含位於Firebase數據庫中的用戶名稱。與cellForItemAt索引路徑單元格下載數據問題

首先,我參考使用自細胞:

let f = FriendCell()

在cellForItemAt indexPath,我定義每個小區和參考從該數據是來自該陣列。然後我引用特定單元格從個人用戶獲取其數據。

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: userResultCellId, for: indexPath) as! FriendCell 

    let user = users[indexPath.row] 

    f.nameLabel.text = user.name 
    print(f.nameLabel.text!) 

    return cell 
} 

當我運行此,控制檯print(f.nameLabel.text!)實際上正確打印的用戶名,但他們似乎沒有傳遞到細胞正常。

我相信正在發生的事情是每個單元格在數據下載之前就已經設置好了,namelabel返回nil,因爲那裏沒有值。但奇怪的是,當我打印該值時,它會正確返回名稱。有什麼建議麼?

回答

4

你不需要行:

let f = FriendCell() 

擺脫這一點。然後在您的cellForItemAt中,您需要設置cell的屬性,而不是f

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: userResultCellId, for: indexPath) as! FriendCell 

    let user = users[indexPath.row] 
    cell.nameLabel.text = user.name 

    return cell 
} 
相關問題