错误无法使类型UICollectionElementKindCell的视图出列
首先尝试使用集合视图并遇到此错误:
由于未捕获的exception’NSInternalInconsistencyException’而终止应用程序,原因:’无法使类型的视图出列:具有标识符的UICollectionElementKindCellCell – 必须为标识符注册一个nib或类或在故事板中连接原型单元’
代码非常简单,如下所示。 我不能为我的生活弄清楚我错过了什么。
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath]; cell.backgroundColor = [UIColor whiteColor]; return cell; }
集合视图控制器是使用nib创建的,委托和数据源都设置为文件的所有者。
View Controller的头文件也非常基本。
@interface NewMobialViewController_3 : UICollectionViewController @end
从deICue方法的UICollectionView文档 :
要点:在调用此方法之前,必须使用registerClass:forCellWithReuseIdentifier:或registerNib:forCellWithReuseIdentifier:方法注册类或nib文件。
您需要在dequeueReusableCellWithReuseIdentifier的参数和UICollectionViewCell的属性之间使用相同的标识符。
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell" forIndexPath:indexPath];
补充@jrtuton写的……你需要的是:
1)注册您的nib文件以将其与您的标识符“连接”:
//MyCollectionView.m - (void) awakeFromNib{ [self registerNib:[UINib nibWithNibName:@"NibFileName" bundle:nil] forCellWithReuseIdentifier: @"MyCellIdentifier"]; }
2)使用您的标识符从笔尖加载自定义单元格:
//MyCollectionView.m - (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath { MyCustomCollectionViewCell* cell = [cv dequeueReusableCellWithReuseIdentifier:@"MyCellIdentifier" forIndexPath:indexPath]; }
3)使用始终静态NSString *以在您的应用程序中再次避免使用相同的标识符。
我已经100%正确地完成了一切。 但由于某种原因,我得到相同的错误一个小时,然后我做的是:
- 去故事板
- 选择原型单元格(在我的情况下)
- 从身份检查器中清除类名,然后重写它
- 在属性检查器中重写标识符名称
简单地说,重做这一步,即使我之前做的正确,它就像xcode错过某个特定纳秒的东西!
我知道这是一个旧的,但我遇到了同样的问题,并希望分享为我修复的问题。
就我而言,我已经宣布了一个全局标识符
let identifier = "CollectionViewCell"
并且在使用之前必须使用自己的权利:
collectionView.dequeueReusableCellWithReuseIdentifier(self.identifier, forIndexPath: indexPath) as! CollectionViewCell
希望这有助于某人:)
您需要在storyboard中提供正确的可重用标识符,您在注册collectionViewCell时已在代码中提供该标识符。
这是代码。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ClctnVC", for: indexPath)as! ClctnVC return cell }
如果您使用的是storyboard而不是xib,那么这里只需几步。
- 确保填写单元格的正确标识符。
-
在ColletionViewDelegate中。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "YourCellName", for: indexPath)as! YourCellName return cell }
-
而已。 您也不想添加registerClass:forCellWithReuseIdentifier:这将导致元素nil错误。
Swift4.0
每当你的UITableViewCell
是xib时,你必须注册UITableView
。
override func viewDidLoad(){ super.viewDidLoad() self.yourtableview.register(UINib(nibName: "yourCellXIBname", bundle: Bundle.main), forCellReuseIdentifier: "YourCellReUseIdentifier") }
如果您通过代码创建它,则必须创建自定义UICollectionViewCell
,然后,初始化UICollectionView
,并使用loadView
设置视图是您创建的UICollectionView
。 如果在viewDidload()
创建,则它不起作用。 此外,您还可以拖动UICollectionViewController
,它可以节省大量时间。