Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between UICollectionViewCell and UICollectionReusableView?

Looking through UICollectionView tutorials, I see some where UICollectionViewCell is subclassed for items, and some where UICollectionReusableView is subclassed instead. The docs don't make it very clear when you would use one over the other.

like image 951
GoldenJoe Avatar asked May 30 '17 17:05

GoldenJoe


People also ask

What is UICollectionReusableView?

A view that defines the behavior for all cells and supplementary views presented by a collection view.

What is UICollectionViewCell?

An object that manages an ordered collection of data items and presents them using customizable layouts.

What is viewForSupplementaryElementOfKind?

collectionView(_:viewForSupplementaryElementOfKind:at:)Asks your data source object to provide a supplementary view to display in the collection view.

How to add header in collectionView?

To display this section header, you'll use UICollectionReusableView . This class is like UICollectionViewCell , except it's usually used to display headers and footers. Like cells, UICollectionView places them in a reuse queue rather than deleting them when they scroll out of the visible bounds.


2 Answers

UICollectionViewCell

A UICollectionViewCell object presents the content for a single data item when that item is within the collection view’s visible bounds. You can use this class as-is or subclass it to add additional properties and methods. The layout and presentation of cells is managed by the collection view and its corresponding layout object.

UICollectionReusableView

The UICollectionReusableView class defines the behavior for all cells and supplementary views presented by a collection view. Reusable views are so named because the collection view places them on a reuse queue rather than deleting them when they are scrolled out of the visible bounds. Such a view can then be retrieved and repurposed for a different set of content.

So the difference is that a UICollectionViewCell presents the content for a single data item and a UICollectionReusableView class defines the behavior for all cells and supplementary views presented by a collection view.

like image 143
Rashwan L Avatar answered Sep 20 '22 19:09

Rashwan L


Actually, Apple recommends that you use UICollectionViewCell both for cells and for supplementary views. That way, you get the useful features such as the background view. UICollectionReusableView is merely what makes these views reusable. So there is no important distinction except for the built-in superclass–subclass relationship.

like image 34
matt Avatar answered Sep 20 '22 19:09

matt