菜鸟教程小白 发表于 2022-12-11 21:46:43

ios - 引用超出范围的 UICollectionViewCell


                                            <p><p>我有一个包含两个单元格的 collectionView。每个单元格都适合屏幕的大小。<br/>
在第一个单元格中,我有一些按钮。<br/>
当我点击一个按钮时,collectionView 会滚动到第二个单元格,根据我点击的按钮,第二个单元格应该显示不同的 View 。</p>

<p>此代码用于在我点击按钮时引用第二个单元格:</p>

<pre><code>if let cell = collectionView.cellForItem(at: secondIndexPath) as? SecondCell {

      // change the view of the cell
         print(&#34;Does not execute code&#34;)
}
</code></pre>

<p>我猜 cellForItem(at: IndexPath) 为 nil,因为单元格超出范围。<br/>
但是我该如何解决这个问题?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>这个</p>

<pre><code>let cell = collectionView.cellForItem(at: secondIndexPath)
</code></pre>

<p>当指定<code>secondIndexPath</code>的cell不可见时返回<code>nil</code>,需要修改model,再次显示时会反射(reflect)到cell , 正弦它的单元格 2 然后</p>

<pre><code>let cellModel = arr// cell 2 of 0 based indexing
cellModel.someProperty = ///
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 引用超出范围的 UICollectionViewCell,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/54200580/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/54200580/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 引用超出范围的 UICollectionViewCell