Crash in CollectionView For Invalid Header - ios

I want to understand what this error means?
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason:
'the view returned from -collectionView:viewForSupplementaryElementOfKind:atIndexPath: was not
retrieved by calling -dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:
for element kind 'UICollectionElementKindSectionHeader' at index path <NSIndexPath: 0x8aeb905cf5be0ed2>
{length = 2, path = 0 - 0}; supplementary view:
<UICollectionReusableView: 0x7f9236dc4ff0; frame = (0 0; 0 0); layer = <CALayer: 0x600001018620>>'
I am using custom header for UICollectionView.I am getting this crash as soon as the view is loaded. even before cellforrowatindexpath is called and the issue is not with the custom header, it is with the return UICollectionReusableView()
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
if kind == UICollectionView.elementKindSectionHeader && indexPath.section == 2
{
return someCustomHeader
}
return UICollectionReusableView()
}

You must always obtain the section header with the method dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:, before calling it you must also register a header class. If you want to show only the header of the third section you must implement collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) to hide unwanted headers by giving them a .zero size (you can' t simply return an instance of UICollectionReusableView for unwanted headers).
import UIKit
class CollectionViewController: UICollectionViewController {
private let headerId = "headerId"
override func viewDidLoad(){
super.viewDidLoad()
// Registers a header class
self.collectionView.register(YourClass.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: self.headerId) // Replace YourClass with the name of your header class
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
// This method must always call dequeueReusableSupplementaryView, even if section!=2
if kind == UICollectionView.elementKindSectionHeader{
return self.collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: self.headerId, for: indexPath)
}
return UICollectionReusableView()
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize{
// Shows only the header of the third section
return section == 2 ? desiredSize : .zero // Replace desiredSize with the size of the visible header
}
}

When you work with custom header you have to register first the header class.
collectionView.register(AppHeaderCollectionView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: AppHeaderCollectionView.headerIdentifier)
and after that for use the header you have to do it this.
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: AppHeaderCollectionView.headerIdentifier, for: indexPath) as! AppHeaderCollectionView
return header
}

After the iOS update to 15.0, I had to subclass UICollectionReusableView for my header view.
guard let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: MenuTitleHeaderView.sectionHeaderID, for: indexPath) as? MenuTitleHeaderView else {
fatalError("Unexpected Sumplementary View")
}
switch kind {
case UICollectionView.elementKindSectionHeader:
...
}
Don't forget to return the size for the header or footer as well.
One more thing, don't forget to return the number of sections. Or you will only see one section display.

Related

How to use custom UICollectionReusableView as section header of collection view?

I've been driven insane for hours as I can't get around with the issue.
I have a collection view which can have different section with different no. of items in each. For each section I need to use a section header of different type. So for this, I'm going to use UICollectionReusableView. But I can't seem to succeed in using a custom subclass of UICollectionReusableView by means of UINib registration.
A crash happens when I downcast the reusable view to my subclass. Like:
let friendHeader = collectionView.dequeueReusableSupplementaryView(ofKind: kind,
withReuseIdentifier: "FriendHeaderView",
for: indexPath) as! FriendHeaderView
Below is the code snippet:
class ViewController: UIViewController {
#IBOutlet weak var collectionView: UICollectionView!
private let viewModel = ProfileViewModel()
override func viewDidLoad() {
super.viewDidLoad()
collectionView.dataSource = self
collectionView.delegate = self
// more code
collectionView.register(UINib(nibName: "FriendHeaderView", bundle: nil),
forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader,
withReuseIdentifier: "FriendHeaderView")
}
}
Now here is the data source implementation:
extension ViewController: UICollectionViewDataSource {
func numberOfSections(in collectionView: UICollectionView) -> Int {
// valid implementation
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// valid implementation
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// valid implementation
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionView.elementKindSectionHeader:
let friendHeader = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "FriendHeaderView", for: indexPath) as! FriendHeaderView
// *** Crash happens here *** //
return friendHeader
default:
assert(false, "Invalid element type")
}
}
}
And I don't know why the collectionView(_:layout:referenceSizeForHeaderInSection:) needs to be also implemented. So here it is:
extension ViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
let size = CGSize(width: collectionView.bounds.width, height: 100)
return size
}
}
Okay, now come to the point: The above mentioned crash doesn't happen
at all if I don't downcast with as! operator. Well, if I use section
header from the storyboard instead of UINib registration, there is
no crash.
If I'm going to need multiple type header, then I can't also use storyboard approach or without down-casting approach as I need to feed data to those headers.
What can I do to have multiple type headers with view built from interface builder?
I've made a demo project with what I've said above. If anyone is interested please check out that.
Once you assign proper class and identifier in your Xib file, then it will work without crashes.
Well, after some more investigation and the input from #good4pc in the accepted answer (actually I found out that by myself before looking at the answer) it seems that the issue is actually happening for some unwanted issue with Xcode.
When we create any view (preferably, UITableViewCell or UICollectionViewCell) with .xib, the class identity is provided automatically for that .xib in the identity inspector. But this was not the case for UICollectionReusableView. See the attached screenshot below for easy understanding.
This is a UICollectionViewCell subclassed with .xib:
This is a UICollectionReusableView subclassed with .xib:
So the key is to provide the class identity of the .xib file which
is done from the attributes inspector.

Moving UICollectionViewCell causes Error for invalidateItemsAtIndexPaths

When dragging to move cells in a UICollectionView, the app is crashing due to items being marked as invalidated, but then missing at the index path.
The code successfully confirms performs the move in the collection. Then the collection returns the expected number of sections and the cells for the section. After seemingly completing without issue, the following error occurs:
2018-12-20 15:39:54.216391-0500 TestApp[1748:485235] *** Assertion failure in -[UICollectionViewData invalidateItemsAtIndexPaths:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKitCore/UIKit-3698.93.8/UICollectionViewData.m:166
2018-12-20 15:39:54.216878-0500 TestApp[1748:485235] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempting to invalidate an item at an invalid indexPath: {length = 2, path = 0 - 1} globalIndex: 1 numItems: 0'
The code itself isn't doing anything very interesting. It makes sure that the last cell in edit mode (an add button) isn't moved, and then updates the data for our model.
It doesn't seem like any of the items in the collection should be invalidated in the first place. The number of items in the collection is fairly small, so everything is onscreen the entire time so no cells are being removed from view in this scenario. No content for any of the cells was changed, so I'm not entirely sure what could have been marked as invalid, but even with that occurring, I'm unaware why it would be missing.
Code below:
// MARK: UICollectionViewDataSource Functions
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
let friendsCount = AppDataModel.sharedInstance.groupModels[groupIndex!].friends.count
if friendsCount >= 0 {
return isEditingEnabled ? friendsCount + 1 : friendsCount
} else {
return 0
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if isEditingEnabled && indexPath.row == AppDataModel.sharedInstance.groupModels[groupIndex!].friends.count {
if !isUpgraded {
var currentVideoCount = 0
for i in 0..<AppDataModel.sharedInstance.groupModels.count {
currentVideoCount += AppDataModel.sharedInstance.groupModels[i].friends.count
}
if currentVideoCount >= maxFreeVideos {
print("Max Videos for trial reached.")
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "upgradeCell", for: indexPath)
return cell
}
}
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "addFriendCell", for: indexPath)
return cell
}
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "friendCell", for: indexPath) as! MainViewFriendCollectionViewCell
let friendForCell = AppDataModel.sharedInstance.groupModels[groupIndex!].friends[(indexPath as NSIndexPath).row]
cell.setup(friendForCell, shouldHideDeleteButton: !isEditingEnabled, onDeleteFriend: self.onDeleteFriend, onForceUpload: self.onForceUpload)
return cell
}
func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
// The user cannot reorder the add button
let addButtonIndex = AppDataModel.sharedInstance.groupModels[groupIndex!].friends.count
let destinationIndex = (destinationIndexPath.item >= addButtonIndex) ? addButtonIndex - 1 : (destinationIndexPath as NSIndexPath).item
// reflect the changes to the view in the model
AppDataModel.sharedInstance.groupModels[groupIndex!].updateFriendOrdersAfterReorder((sourceIndexPath as NSIndexPath).item, toIndex: destinationIndex)
}
func collectionView(_ collectionView: UICollectionView, targetIndexPathForMoveFromItemAt originalIndexPath: IndexPath, toProposedIndexPath proposedIndexPath: IndexPath) -> IndexPath {
let addButtonIndex = AppDataModel.sharedInstance.groupModels[groupIndex!].friends.count
return proposedIndexPath.item >= addButtonIndex ? originalIndexPath : proposedIndexPath
}

Get next cell in UIcollectionView in Swift

I need to get the next cell inside cellForItem within a collection view so that I can update a view object. When I try the following below it doesn't work. I've also tried indexPathForVisibleItems passing in indexPath.row + 1 and the produces an index out of range error.
let index = IndexPath(row: indexPath.row + 1, section: indexPath.section)
if let nextCell = collectionView.cellForItem(at: index) as! MKRCell {
nextCell.setupWaitView(time: timeToWait)
nextCell.waitViewHeightConstraint.constant = 80
nextCell.waitView.alpha = 1
nextCell.waitView.isHidden = false
}
Is this possible to achieve or will I need to do this via another way?
Thanks
No, it is not possible to get the cell object before initialization in cellForItemAt but here
you can receive the call before displaying the cell from UICollectionViewDelegate
func collectionView(_ collectionView: UICollectionView,willDisplay cell: UICollectionViewCell,forItemAt indexPath: IndexPath) {
if let myCell = cell as? MKRCell {
}
}
AND
If you want to set up the cell you have to setup view in the UICollectionViewDataSource
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
}
You should update the cell in:
func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
Remember to modify only the cell you'll be returning from this method. Other cells might not have exist at that moment.
Alternatively you can keep a weak reference to the cell and update it when needed.

Crashing while collection view cell deletion because of "UICollectionViewLayoutAttributes"

My data source is a 2-dimensional array because I have emojis divided into sections.
eg. [[a,b,c,d],[e,f,g],[h,j,k,l,m]]
My cell deletion code -
func deleteEmoji(at indexPath: IndexPath) {
// Get the bad emoji
let emojiToBeDeleted = emojisArray[indexPath.section][indexPath.row]
// Delete the emoji from datasource and collection view
emojisArray[indexPath.section].remove(at: indexPath.row)
stickerCollectionView.deleteItems(at: [indexPath])
// Delete the section from datasource and collection view as its emoji count is zero
if emojisArray[indexPath.section].count == 0 {
emojisArray.remove(at: indexPath.section)
stickerCollectionView.deleteSections(IndexSet(integer: indexPath.section)) // Crashing here :(
sectionCollectionView.reloadData()
}
// Delete the bad emoji
emojiToBeDeleted.delete()
// Undo the deletion mode as all emoji's are deleted
if emojisArray.count == 0 {
isDeletionMode = false
}
}
I also have footers for every section, except for the last section, for that I have this in UICollectionViewDataSource
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionElementKindSectionFooter:
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: Identifier.footer, for: indexPath)
return headerView
default:
assert(false, "Unexpected element kind")
}
}
and in UICollectionViewDelegate
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
switch collectionView {
case stickerCollectionView:
if section == emojisArray.count - 1 {
return CGSize.zero
}
return CGSize(width: collectionView.bounds.size.width, height: 15)
default:
return CGSize.zero
}
}
Error -
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'no
UICollectionViewLayoutAttributes instance for
-layoutAttributesForSupplementaryElementOfKind: UICollectionElementKindSectionFooter at path {length = 2, path = 0 - 0}'
I am getting this error while deleting cells. And crash doesn't happen always, it is quite random.
sectionCollectionView is completely different UICollectionView.

Assertion failure in -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:]

I am creating a table view and inside a custom UItableviewCell I am using collection view and I also create a custom collectionview cell. After setting all the basic functionalities of collection view in TableViewCell, When run the app I got the crash with reason:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'the view returned from -collectionView:cellForItemAtIndexPath: ( {length = 2, path = 0 - 0}) was not retrieved by calling -dequeueReusableCellWithReuseIdentifier:forIndexPath: or is nil (>)'
I tried to search more for it but can't find any direction
Here is my code snippet:
1. In TableViewcell awakeFrom Nib:
override func awakeFromNib() {
super.awakeFromNib()
// self.collectionView_Files.registerNib(UINib(nibName: "MediaCollectionCell", bundle: nil), forCellWithReuseIdentifier: "MediaCollectionCell")
self.collectionView_Files.registerClass(MediaCollectionCell.self, forCellWithReuseIdentifier: "MediaCollectionCell")
}
CollectionView methods:
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return arrFolderData.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let simpleTableIdentifier = "MediaCollectionCell"
var cell: MediaCollectionCell = collectionView.dequeueReusableCellWithReuseIdentifier(simpleTableIdentifier, forIndexPath: indexPath) as! MediaCollectionCell
cell = NSBundle.mainBundle().loadNibNamed(simpleTableIdentifier, owner: self, options: nil)[0] as! (MediaCollectionCell)
let dict = arrFolderData[indexPath.row]
if(dict["file_type"] as! String == "0") { /// Means image
cell.imgView_Item.image = UIImage(named: "images_41")
cell.btn_ViewImage.hidden = false
cell.btn_PlayVideo.hidden = true
} else if (dict["file_type"] as! String == "1") { /// Means video
cell.btn_ViewImage.hidden = true
cell.btn_PlayVideo.hidden = false
cell.imgView_Item.image = UIImage(named: "video_thumb_icon")
}
// cell.backgroundColor = arrFolderData[indexPath.item]
return cell
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
print("Collection view at row \(collectionView.tag) selected index path \(indexPath)")
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
{
let length = (UIScreen.mainScreen().bounds.width-15)/2
return CGSizeMake(length,length);
}
As mentioned in the post, I was working in tablecell xib file and try to do as I need, however once I change my approach and I create tableview cell into the UITableView inside the storyboard and update all the outlets and then run the app. The app was working fine....all the outlets in collection view cell are dynamically created with taking reference from their static variables.

Resources