菜鸟教程小白 发表于 2022-12-11 16:47:51

ios - Swift 2,滚动后的 UiCollectionview - 更改单元格数据


                                            <p><p>对于单元格,我只是从数组中输入信息:</p>

<pre><code>func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -&gt; UICollectionViewCell {

            let cell = collectionView.dequeueReusableCellWithReuseIdentifier(&#34;Cell&#34;, forIndexPath: indexPath) as! ImagesCollectionViewCell

            if cell.dateLabel.text == &#34;&#34; {
                cell.dateLabel.text = imagesDatas.datesStr
                cell.image.image = imagesDatas.images
            }
            return cell
      }
</code></pre>

<p>为了数组,我在 DidAppear 中生成数据。只有一次。
但是当我滚动 collectionView - 单元格更改数据时,例如:</p>

<p>1 个单元格 - 图片A</p>

<p>2 单元格 - 图片B</p>

<p>然后滚动,在collectionview中会看到:</p>

<p>1 个单元格 - 图片B</p>

<p>2 单元格 - 图片A </p>

<p>然后滚动,在collectionview中又会看到:</p>

<p>1 个单元格 - 图片A</p>

<p>2 单元格 - 图片B</p>

<p>无法理解这里发生了什么...... </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><pre><code>override func prepareForReuse() {
    super.prepareForReuse()

      cell.dateLabel.text = nil
      cell.image.image = nil
</code></pre>

<p>}</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - Swift 2,滚动后的 UiCollectionview - 更改单元格数据,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/37963558/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/37963558/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - Swift 2,滚动后的 UiCollectionview - 更改单元格数据