こんにちは
UICollectionViewにて下記のように条件分岐を用いた2重読み込み防止のコードを書きました。
書いたと思っていたのですが、スクロールし、セルの描画領域が画面外に出ると、画面外に出たセルの並び順が変化してしまいます。
これの解決方法をご存知の方教えてください。
別にインスタンスは再生成しているわけでもないと思います。
swift
1//numberは初期が0です。 2//photosは配列で画像パスがはいってると仮定しています。 3 4 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 5 let number = cell.contentView.viewWithTag(1) as! UIImageView //状態を調べるためのnumber 6 if number == 0{//セルのnumberが0の時 7 number = 1 // numberを1にする 8 let cell = testCollectionView.dequeueReusableCell(withReuseIdentifier: "testCell", for: indexPath) as! testCell 9 let testImage = cell.contentView.viewWithTag(2) as! UIImageView 10 let img = UIImage(named: photos[indexPath.row])//画像をセル内のImageViewに入れる 11 }else{ 12 print("Nothing") 13 } 14 return cell//セルを出力 15 }
質問のコード、エラー出てませんか?
あなたの回答
tips
プレビュー