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

ios - 无法将 UICollectionView 的单元格所需的 View 出列


                                            <p><p>我有一个带有自定义类的 UICollectionView,它是 UICollectionViewCell 的子类。我不断收到以下错误:</p>

<blockquote>
<p>reason: &#39;could not dequeue a view of kind:
UICollectionElementKindCell with identifier Appointment - must
register a nib or a class for the identifier or connect a prototype
cell in a storyboard&#39;</p>
</blockquote>

<p>然而,我做了以下事情:</p>

<p>用collectionView注册了自定义类。 </p>

<pre><code>self.collectionView.registerClass(AppointmentCollectionViewCell.self, forCellWithReuseIdentifier:&#34;Appointment&#34;);
</code></pre>

<p>这是我的出队行:</p>

<pre><code>let appointmentCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier(&#34;Appointment&#34;, forIndexPath: indexPath) as! AppointmentCollectionViewCell;
</code></pre>

<p>这就是我的 AppointmentCollectionViewCell 的样子:</p>

<pre><code>class AppointmentCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var leftBorderView: UIView!
    @IBOutlet weak var appointmentLabel: UILabel!

    override func awakeFromNib() {
      super.awakeFromNib()
    }

    func setAppointment(appointment:Appointment){
      self.leftBorderView.backgroundColor = appointment.eventColour;
      self.appointmentLabel.textColor = appointment.eventColour;
      self.backgroundColor = appointment.eventColour.colorWithAlphaComponent(0.2);
    }

}
</code></pre>

<p>我还在 View 中提供了重用标识符为“约会”。</p>

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

<p>我还根据此处发布的答案尝试了以下方法 - 仍然无法正常工作:</p>

<pre><code>let appointmentNib = UINib(nibName: &#34;AppointmentCollectionViewCell&#34;, bundle:NSBundle.mainBundle())
      self.collectionView.registerNib(appointmentNib, forCellWithReuseIdentifier: &#34;Appointment&#34;)
</code></pre>

<p>谷歌搜索后,大多数人忘记注册类(class)。就我而言,我已经注册了类(class),但仍然不断收到错误消息。我哪里错了?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>如果您的单元格与 xib 文件相关联,您需要使用 registerNib 方法,而不是 registerClass 方法。</p>

<pre><code>func registerNib(_ nib: UINib?, forCellWithReuseIdentifier identifier: String)
</code></pre>

<p> <a href="https://developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionView_class/#//apple_ref/occ/instm/UICollectionView/registerNib:forCellWithReuseIdentifier:" rel="noreferrer noopener nofollow">Documentation</a> </p>

<p>您似乎也混淆了 Collection Reusable View 和 UICollectionViewCell - 它是什么?两者都有特定的注册方法,查看文档。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 无法将 UICollectionView 的单元格所需的 View 出列,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/39236530/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/39236530/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 无法将 UICollectionView 的单元格所需的 View 出列