Taking first plunge with collection views and am running into this error:
Terminating app due to uncaught exception \'NSInternalInconsistencyException
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")
}
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 self right before using it:
collectionView.dequeueReusableCellWithReuseIdentifier(self.identifier, forIndexPath: indexPath) as! CollectionViewCell
Hope this helps someone :)
I had the same problem.
When I created a CollectionViewController, the default reuseIdentifier
was Cell
with capital C
, but I made a mistake and made my cell identifier in Storyboard
to be cell
They must be the same.
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.
If you are using storyboard instead of xib, here are few steps.
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't want to add registerClass:forCellWithReuseIdentifier: which will cause element nil error.
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.