2017-07-18 10 views
1

firebaseデータベースにあるユーザー名を含むUIColelctionViewセルがあります。cellForItemAt indexPathセルのダウンロードに関するデータ

まず私が使用してカスタムセルを参照:cellForItemAt indexPathで

let f = FriendCell()

を、Iは、各セルを定義し、データから来された配列を参照します。次に、特定のセルがデータを取得する個々のユーザーを参照します。私はこれを実行すると

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を返すことです。しかし、奇妙なのは、値を出力すると正しく名前が返されるということです。助言がありますか?

let f = FriendCell() 

がその取り除く:

答えて

4

あなたはラインを必要としません。その後、cellForItemAtでは、ではなく、cellのプロパティを設定する必要があります。

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 
} 
関連する問題