2017-10-04 8 views
-1

最近、この問題に対処するのに問題がありました。 2つのUICollectionViewCellが用意されていて、オブジェクトが配列に格納されています。UICollectionView - forループでUICollectionViewCellを返します

私はreturn文を持っていますが、エラーメッセージは表示され続けます。私は問題を解決するために何が起こってもセルを返さなければならないことを知っています。しかし、これは私の職務で書いた唯一のコードであり、何をすべきかわからない。

How can I fix the error message?

+3

コードを画像として投稿しないでください。コードを質問にコピーし、コードブロックを使用してフォーマットします。 – the4kman

+2

ループをまったく持ってはいけません。 'indexPath'に基づいてデータを取得します。 – rmaddy

+0

私は前にそのコードを見ました。私はまだあなたがなぜループをしているのか理解できません。 'friendList [0]'は同じことをする必要があるからです。そして、私は 'friendList'がなぜ配列であるのか分かりません。 – Larme

答えて

0

エラーは、とにかく不要ですforループ、の表示されます。実際のインデックスパスの行に対応する要素にアクセスする必要があります。

forループを削除し、あなたの関数の先頭にこれを追加:

let friend = friends[indexPath] 
+1

最後の行は 'index'に基づいていて、' 0'にハードコードされていないことが必要です。 – rmaddy

+0

配列インデックスとして 'IndexPath'インスタンスを使用することはできません。 – rmaddy

+0

さて、私はそれを取得します。しかし、私はまた、オブジェクトを通過し、1つのカスタムセルに1を持ち、2つをカスタムセルに持つものをすべて入れたいと思う。それをどうすれば実現できますか? –

1

friendShipListが0の要素を持つことができ、あなたがそのエラーを削除したい場合は、ループの外returnステートメントを配置する必要があります。また、そこにループを張るのは本当に悪い考えです。あなたは、アレイ内のいくつかの要素にアクセスする必要がある場合は、行うことができます:

let friend = friends[indexPath.row] 

を私はちょうどあなたが友人のリストを表示しようとしていると仮定しています。

+0

さて、私はそれを得る。しかし、私はまた、オブジェクトを通過し、1つのカスタムセルに1を持ち、2つをカスタムセルに持つものをすべて入れたいと思う。それをどうすれば実現できますか? –

0

これはあなたの新しくQuestion that is here

の回答は、これはあなたの問題を解決します。このコードを試してみてくださいです。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    friendsList.sort { (first: Friends, second: Friends) -> Bool in 
     first.confirmed < second.confirmed 
    } 

    let friend = friendsList[indexPath.row] 
    if (indexPath.section == 0) { 
     if friend.confirmed == 1 { 
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "addDiscoverCell", for: indexPath) as! addDiscoverCell 
      cell.profileImageView.image = nil 
      cell.profileLabel.text = friend.username 
      cell.partyLabel.text = "" 
      return cell 
     } 
     else if friend.confirm == 2 { 
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "addDiscoverCell", for: indexPath) as! addDiscoverCell 
      cell.profileImageView.image = nil 
      cell.profileLabel.text = "" 
      cell.partyLabel.text = "" 
      return cell 
     } 
     else 
     { 
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "addDiscoverCell", for: indexPath) as! addDiscoverCell 
      cell.profileImageView.image = nil 
      cell.profileLabel.text = "" 
      cell.partyLabel.text = "" 
      return cell 
     } 
    } else { 
     if friend.confirmed == 2 { 
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "discoverCell", for: indexPath) as! discoverCell 
      cell.profileImageView.image = nil 
      cell.profileLabel.text = friend.username 
      cell.partyLabel.text = "" 
      return cell 
     } 
     else if friend.confirm == 1 { 
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "discoverCell", for: indexPath) as! discoverCell 
      cell.profileImageView.image = nil 
      cell.profileLabel.text = "" 
      cell.partyLabel.text = "" 
      return cell 
     } 
     else 
     { 
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "discoverCell", for: indexPath) as! discoverCell 
      cell.profileImageView.image = nil 
      cell.profileLabel.text = "" 
      cell.partyLabel.text = "" 
      return cell 
     } 
    } 
} 
関連する問題