ios 错误无法使 UICollectionElementKindCell 类型的视图出列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15058975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error could not dequeue a view of kind UICollectionElementKindCell
提问by Anti-Dentite
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'
由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法使类型的视图出列:带有标识符 Cell 的 UICollectionElementKindCell - 必须为标识符注册一个笔尖或类,或连接故事板中的原型单元格”
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.
集合视图控制器是使用 nib 创建的,并且委托和数据源都设置为文件的所有者。
View Controller's header file is also really basic.
视图控制器的头文件也非常基础。
@interface NewMobialViewController_3 : UICollectionViewController <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@end
回答by jrturton
From the UICollectionView documentationfor the dequeue method:
从出队方法的UICollectionView 文档中:
Important:You must register a class or nib file using the registerClass:forCellWithReuseIdentifier: or registerNib:forCellWithReuseIdentifier: method before calling this method.
重要提示:在调用此方法之前,您必须使用 registerClass:forCellWithReuseIdentifier: 或 registerNib:forCellWithReuseIdentifier: 方法注册一个类或 nib 文件。
回答by Kohei Mikami
You need to use same identifier between the dequeueReusableCellWithReuseIdentifier's argument and the UICollectionViewCell's property.
您需要在 dequeueReusableCellWithReuseIdentifier 的参数和 UICollectionViewCell 的属性之间使用相同的标识符。
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell" forIndexPath:indexPath];
回答by orafaelreis
Complementing what @jrtuton written... What you need is:
补充@jrtuton 所写的内容......你需要的是:
1) Register your nib file to "connect" it with your identifier:
1) 注册您的 nib 文件以将其与您的标识符“连接”:
//MyCollectionView.m
- (void) awakeFromNib{
[self registerNib:[UINib nibWithNibName:@"NibFileName" bundle:nil] forCellWithReuseIdentifier: @"MyCellIdentifier"];
}
2) Use your identifier to load your custom cell from a nib:
2) 使用您的标识符从笔尖加载您的自定义单元格:
//MyCollectionView.m
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
MyCustomCollectionViewCell* cell = [cv dequeueReusableCellWithReuseIdentifier:@"MyCellIdentifier" forIndexPath:indexPath];
}
3) Use always static NSString* to avoid the same identifiers again in your app.
3) 始终使用静态 NSString* 以避免在您的应用程序中再次使用相同的标识符。
回答by Omar N Shamali
i had everything 100% done correctly. but for some reason i got the same error for an hour, then what i did is:
我已经 100% 正确完成了所有事情。但出于某种原因,我在一个小时内遇到了同样的错误,然后我所做的是:
- go to storyboard
- select the prototype cell (in my case)
- clear the class name from identity inspector, then rewrite it
- rewrite the identifier name in the attributes inspector
- 转到故事板
- 选择原型单元格(在我的情况下)
- 从身份检查器中清除类名,然后重写它
- 在属性检查器中重写标识符名称
simply, redid this step even tho i made it correct before, its like xcode misses something at a specific nanosecond!
简单地说,即使我之前做对了,也要重做这一步,就像 xcode 在特定的纳秒内错过了一些东西!
回答by Tech Luthiers
Swift 5
斯威夫特 5
1) Make sure you have a correct deque for HEADER, you might be using the regular for normal cells.
1)确保您有正确的 HEADER 双端队列,您可能正在使用常规单元格。
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerId, for: indexPath)
return header
}
2) doubleCheck the registration (ViewDidLoad)
2)doubleCheck注册(ViewDidLoad)
collectionView.register(HeaderCell.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: headerId)
回答by Allen
If you are using storyboardinstead of xib, here are few steps.
如果您使用的是故事板而不是 xib,这里有几个步骤。
- Making sure you fill the right identifier of your cell.
- 确保填写正确的单元格标识符。
In the ColletionViewDelegate.
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "YourCellName", for: indexPath)as! YourCellName return cell }
That's it. You also don'twant to add registerClass:forCellWithReuseIdentifier:which will cause element nil error.
在ColletionViewDelegate 中。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "YourCellName", for: indexPath)as! YourCellName return cell }
就是这样。您还没有要添加的registerClass:forCellWithReuseIdentifier:这将导致元素零误差。
回答by Rakesh Patel
Swift4.0
斯威夫特4.0
Whenever your UITableViewCell
is xibat that time you must have to register with UITableView
.
每当您当时UITableViewCell
是xib时,您都必须注册UITableView
.
override func viewDidLoad(){
super.viewDidLoad()
self.yourtableview.register(UINib(nibName: "yourCellXIBname", bundle: Bundle.main), forCellReuseIdentifier: "YourCellReUseIdentifier")
}
回答by ciara staggs
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.
为我解决这个问题的方法(大致)与 Omar 的答案相同,除了我最终创建了一个新的 UICollectionViewCell 自定义类并为 Cell Reuse Indentifier 提供了一个新的/不同的名称,而不是我在应用程序中的其他地方使用的名称。在那之后它立即起作用了。
回答by Jacob.B
I know this is an old one, but I've experienced the same problem and wanted to share what fixed it for me.
我知道这是一个旧问题,但我遇到了同样的问题,并想分享为我解决的问题。
In my case, I've declared a global identifier
就我而言,我已经声明了一个全局标识符
let identifier = "CollectionViewCell"
and had to use selfright before using it:
并且必须在使用之前使用self:
collectionView.dequeueReusableCellWithReuseIdentifier(self.identifier, forIndexPath: indexPath) as! CollectionViewCell
Hope this helps someone :)
希望这对某人有所帮助:)
回答by Doci
Just in case, if you are working with storyboard set collectionView identifier in the right place in the Attributes Inspector -> Identifier field. Not under the class name in "Restoration ID".
以防万一,如果您在 Attributes Inspector -> Identifier 字段中的正确位置使用 storyboard set collectionView identifier。不在“恢复 ID”中的类名下。
If you are using collection view in tableView cell, add delegates to tableView cell not in the tableViewController.
如果您在 tableView 单元格中使用集合视图,请将委托添加到 tableView 单元格而不是在 tableViewController 中。