无法将需要的视图作为单元格退出UICollectionView

问题描述:

我有一个UICollectionView,它具有UICollectionViewCell子类的自定义类。我不断收到以下错误:无法将需要的视图作为单元格退出UICollectionView

reason: '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'

我已经做不过以下几点:

注册自定义类用的CollectionView。

self.collectionView.registerClass(AppointmentCollectionViewCell.self, forCellWithReuseIdentifier:"Appointment"); 

这里是我出列线:

let appointmentCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("Appointment", forIndexPath: indexPath) as! AppointmentCollectionViewCell; 

这是我AppointmentCollectionViewCell是什么样子:

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); 
    } 

} 

我也曾经在视图中提供的再利用标识为“约会” 。

enter image description here

我也试过以下基于此公布答案 - 仍然没有工作:

let appointmentNib = UINib(nibName: "AppointmentCollectionViewCell", bundle:NSBundle.mainBundle()) 
     self.collectionView.registerNib(appointmentNib, forCellWithReuseIdentifier: "Appointment") 

谷歌搜索后,多数人忘记了注册类。在我的情况下,我已经注册了课程,但仍然不断收到错误。我哪里错了?

+0

你有正确的界面生成器中键入 '约会'?在故事板中选择单元格后,打开属性检查器并检查标识符字段...? – pedrouan

+0

@pedrouan,我已根据您的意见更新了帖子 – user481610

+0

故事板中的单元标识符不是必需的如果您以编程方式注册单元标识符 – Tiko

如果您的单元格与xib文件关联,则需要使用registerNib方法,而不是registerClass方法。

func registerNib(_ nib: UINib?, forCellWithReuseIdentifier identifier: String) 

Documentation

也似乎是混乱的收集可重复使用的查看和UICollectionViewCell - 这是什么呢?两者都有特定的注册方法,请查看文档。

+0

我不遵循上次发布的更新,“您似乎也很迷惑可重用集合查看和UICollectionViewCell“请详细说明,我可能会困惑的东西 – user481610

先检查:

  1. 你有厦门国际银行,其具有类AppointmentCollectionViewCell

  2. 从 类例如获取笔尖 UINib *favouritesNib = [UINib nibFromClass:[TNYCellRestaurantAnchorclass]];

  3. 注册笔尖的CollectionView

    [self.collectionViewName registerNib:nib forCellReuseIdentifier:@"identifier name"];