2017-07-18 64 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 
}