最近、この問題に対処するのに問題がありました。 2つのUICollectionViewCellが用意されていて、オブジェクトが配列に格納されています。UICollectionView - forループでUICollectionViewCellを返します
私はreturn文を持っていますが、エラーメッセージは表示され続けます。私は問題を解決するために何が起こってもセルを返さなければならないことを知っています。しかし、これは私の職務で書いた唯一のコードであり、何をすべきかわからない。
最近、この問題に対処するのに問題がありました。 2つのUICollectionViewCellが用意されていて、オブジェクトが配列に格納されています。UICollectionView - forループでUICollectionViewCellを返します
私はreturn文を持っていますが、エラーメッセージは表示され続けます。私は問題を解決するために何が起こってもセルを返さなければならないことを知っています。しかし、これは私の職務で書いた唯一のコードであり、何をすべきかわからない。
エラーは、とにかく不要ですfor
ループ、の表示されます。実際のインデックスパスの行に対応する要素にアクセスする必要があります。
for
ループを削除し、あなたの関数の先頭にこれを追加:
let friend = friends[indexPath]
friendShipListが0の要素を持つことができ、あなたがそのエラーを削除したい場合は、ループの外returnステートメントを配置する必要があります。また、そこにループを張るのは本当に悪い考えです。あなたは、アレイ内のいくつかの要素にアクセスする必要がある場合は、行うことができます:
let friend = friends[indexPath.row]
を私はちょうどあなたが友人のリストを表示しようとしていると仮定しています。
さて、私はそれを得る。しかし、私はまた、オブジェクトを通過し、1つのカスタムセルに1を持ち、2つをカスタムセルに持つものをすべて入れたいと思う。それをどうすれば実現できますか? –
これはあなたの新しく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
}
}
}
コードを画像として投稿しないでください。コードを質問にコピーし、コードブロックを使用してフォーマットします。 – the4kman
ループをまったく持ってはいけません。 'indexPath'に基づいてデータを取得します。 – rmaddy
私は前にそのコードを見ました。私はまだあなたがなぜループをしているのか理解できません。 'friendList [0]'は同じことをする必要があるからです。そして、私は 'friendList'がなぜ配列であるのか分かりません。 – Larme