菜鸟教程小白 发表于 2022-12-13 05:55:11

ios - UICollectionView 单元格未添加为 UICollectionView 的 subview


                                            <p><p>我是 <code>UICollectionView</code> 的新手。</p>

<p>我关注了<a href="http://www.appcoda.com/ios-programming-uicollectionview-tutorial/" rel="noreferrer noopener nofollow">this tutorial</a>学习如何使用 <code>UICollectionView</code>,但问题是在 <code>.xib</code> 中(不使用 Storyboard )我无法添加 <code>UICollectionViewCell</code>作为 <code>UICollectionView</code> 的 subview 。</p>

<p>在本教程中,单元格是在 <code>.xib</code> 中的 <code>UICollectionView</code> 下自动创建的。</p>

<p>任何帮助将不胜感激。 </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>该教程适用于在 xib 中的 stroy board 中工作,您必须先创建自定义单元格创建 ViewController ,然后将 UicollectionView 添加到 ViewController </p>

<p> <img src="/image/xvZVQ.png" alt="enter image description here"/>
<img src="/image/Naslc.png" alt="enter image description here"/> </p>

<p>然后为集合单元创建自定义类</p>

<p> <img src="/image/DxHv8.png" alt="enter image description here"/> </p>

<p>然后创建空 xib 资源并添加 Collection View 单元格
<img src="/image/elMew.png" alt="enter image description here"/>
<img src="/image/IrD4Q.png" alt="enter image description here"/>
<img src="/image/pKy8I.png" alt="enter image description here"/> </p>

<p>并为该控件设置类名和标识符
<img src="/image/q5IId.png" alt="enter image description here"/> </p>

<p>然后创建委托(delegate)方法以将自定义单元格添加到您的 collectionViewController</p>

<pre><code>-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *cellIdentifier = @&#34;customCell&#34;;

    customCell *cell = ;

    return cell;

}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - UICollectionView 单元格未添加为 UICollectionView 的 subview ,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/20994378/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/20994378/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - UICollectionView 单元格未添加为 UICollectionView 的 subview