Error could not dequeue a view of kind UICollectio

2020-02-10 00:01发布

Taking first plunge with collection views and am running into this error:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

The code is very simple, as shown below. I can't for the life of me figure out what it is that I'm missing.

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor]; 
return cell;
}

The collection view controller was created using a nib and the delegates & datasources are both set to file's owner.

View Controller's header file is also really basic.

@interface NewMobialViewController_3 : UICollectionViewController <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@end

12条回答
小情绪 Triste *
2楼-- · 2020-02-10 00:36

If you create it by code, you must create a custom UICollectionViewCell, Then, init a UICollectionView ,and use loadView to set the view is the UICollectionView that you create. If you create in viewDidload() , it does not work. In addition, you can also drag a UICollectionViewController, it saves a lot of time.

查看更多
Melony?
3楼-- · 2020-02-10 00:38

You need to use same identifier between the dequeueReusableCellWithReuseIdentifier's argument and the UICollectionViewCell's property.

UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell" forIndexPath:indexPath];

identifier

查看更多
爷的心禁止访问
4楼-- · 2020-02-10 00:38

If you are using storyboard instead of xib, here are few steps.

  1. Making sure you fill the right identifier of your cell.

Prototype cellAttribute panel

  1. In the ColletionViewDelegate.

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "YourCellName", for: indexPath)as! YourCellName
        return cell            
    }
    
  2. That's it. You also don't want to add registerClass:forCellWithReuseIdentifier: which will cause element nil error.

查看更多
该账号已被封号
5楼-- · 2020-02-10 00:38

Swift4.0

Whenever your UITableViewCell is xib at that time you must have to register with UITableView.

override func viewDidLoad(){
    super.viewDidLoad()

    self.yourtableview.register(UINib(nibName: "yourCellXIBname", bundle: Bundle.main), forCellReuseIdentifier: "YourCellReUseIdentifier")
   }
查看更多
神经病院院长
6楼-- · 2020-02-10 00:44

From the UICollectionView documentation for the dequeue method:

Important: You must register a class or nib file using the registerClass:forCellWithReuseIdentifier: or registerNib:forCellWithReuseIdentifier: method before calling this method.

查看更多
啃猪蹄的小仙女
7楼-- · 2020-02-10 00:44

What fixed this for me was (roughly) same as Omar's answer, except I ended up creating a new UICollectionViewCell custom class and giving the Cell Reuse Indentifier a new / different name than the one that I had used elsewhere in the application. It worked immediately after that.

查看更多
登录 后发表回答