Collection View updates in iOS10 Part 2

Jun 25, 2016 16:39 · 276 words · 2 minute read

Along with prefetching, many of the other changes to UICollectionView introduced with iOS10 relate to performance improvements.

The lifecycle of a cell has been well-established since UICollectionView was introduced in iOS6:

prepareForReuse is called first, and is where the cell’s contents should be set back to their default state

cellForItemAtIndexPath: is where the majority of the work involved in setting up the cell takes place

willDisplayCell: is a last-minute opportunity to update the cell before it’s displayed

didEndDisplayingCell: is called as the cell disappears from view

The four lifecycle functions remain in iOS10, but there have been some subtle changes to when they’re called:

  • willDisplayCell: is now called later than previously - whereas in iOS9 and earlier the function was called immediately the cell was taken off the recycle queue, in 10 it’s not called until just before the cell is displayed
  • previously, didEndDisplayingCell was called as soon as the cell scrolled out of the visible bounds of the collection view. Now there’s a delay, which means that the cell hangs around a bit longer in case it’s scrolled back in again. This removes the need to recreate the cell from scratch, and should improve performance when changing scroll direction.

The other significant change is that cells are now dequeued individually rather than row-by-row. This helps to spread out the load of setting each cell up, rather than causing spikes in load; and also aids with making sure that cells are always ready to be displayed.

The good news is that all these changes take place under the hood, so you don’t need to make changes to pre-existing code in order to take advantage of them.