Beginning Collection Views · Challenge: Enhance Section Headers | Ray Wenderlich


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/6308-beginning-collection-views/lessons/17

After completing the exercise, I found that if I added a new item it would not update the count label in the section header. For those wondering how to fix this issue, I added the following code to the addItem function in MainViewController.swift file.

@IBAction func addItem() {
		let index = dataSource.indexPathForNewRandomPark()
		collectionView?.insertItems(at: [index])
        
        // Updates the count number in the section header, without fading animation
        UIView.performWithoutAnimation {
            collectionView.reloadSections(IndexSet(0 ..< dataSource.numberOfSections))
        }
	}

If you do not put the UIView.performWithoutAnimation, you would notice there is a fade animation when adding a new item. So depending on what you like, feel free to choose to do with it or without.

Just wanted to note that, in the following video when you subclass UICollectionViewLayout to enable the animation for adding an item. What I had suggested above with UIView.performWithoutAnimation{} will disable that.

After completing the exercise on subclassing UICollectionViewLayout, I’m still having an issue with eliminating the fading animation from using collectionView.reloadSections(IndexSet(0 …< dataSource.numberOfSections)).

Any suggestions on how to solve would be greatly appreciated.

@bdmoakley Can you please help with this when you get a chance? Thank you - much appreciated! :]

This looks like a bug in the project. This is something we’re looking at and will update in the next version of the course.

Hi Brian, I’ve got a quick question: Why not implement the Section class into DataSource, put the logic for Section objects into their own class and initialize new Section objects with a dedicated initializer, passing in the IndexPath from the (:viewForSupplementaryElementOf:at:) method?
I might be missing something here, but the approach in this video appears somewhat counterintuitive given the prior best practice advice to contain object logic within its own class.

@bdmoakley Can you please help with this when you get a chance? Thank you - much appreciated! :]