菜鸟教程小白 发表于 2022-12-11 17:13:36

ios - CollectionView 错误 : must pass a valid reuse identifier


                                            <p><p>我正在尝试使用 Storyboard中的 UICollectionView 原型(prototype)单元,就像我们对 UITableView 所做的那样。但是 <code>collectionView.dequeueReusableCellWithReuseIdentifier(:, forIndexPath:)</code> 导致应用崩溃并抛出错误</p>

<blockquote>
<p>*** Terminating app due to uncaught exception &#39;NSInvalidArgumentException&#39;, reason: &#39;must pass a valid reuse
identifier to -[UICollectionView
dequeueReusableCellWithReuseIdentifier:forIndexPath:]&#39;</p>
</blockquote>

<p>UICollectionView 需要做什么?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>只需选择您的单元格 (CollectionViewCell) 并在 <strong>Attributes Inspector</strong> 中为其设置标识符名称:</p>

<p> <a href="/image/X1r1L.png" rel="noreferrer noopener nofollow"><img src="/image/X1r1L.png" alt="enter image description here"/></a> </p>

<p>然后在代码中使用相同的标识符:</p>

<pre><code>let cell = collectionView.dequeueReusableCellWithReuseIdentifier(&#34;item&#34;, forIndexPath: indexPath)
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - CollectionView 错误 : must pass a valid reuse identifier,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/38857082/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/38857082/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - CollectionView 错误 : must pass a valid reuse identifier