Skip to content

UICollectionViewItemEvent ​

UICollectionViewItemEvent represents something that happened to one item in a list: it was selected, deselected, is about to be displayed, or is no longer displayed.

Four events of UICollectionView are this class, and indexPath is the position of the item.

Inheritance: Event → UICollectionViewItemEvent

Constructor ​

UICollectionViewItemEvent()

Creates a UICollectionViewItemEvent. The runtime creates one itself when it fires the events below; app code can use it to build an event object and fire it with dispatchEvent() to try out a listener.

Instance properties ​

Also inherits the properties of Event; the most commonly used are type and target. The properties below are read-only; assigning to them throws TypeError.

PropertyDescription
indexPath read-onlyThe position of the item when the event happened

These properties live on the prototype, not on the event object itself: Object.keys(event) and { ...event } don't include them, so read them one by one. See Events with fields.

Events that use this class ​

ObjectEvents
UICollectionViewitemselect, itemdeselect, beforedisplay, displayend

It can't be canceled: cancelable is false, and preventDefault() has no effect.

Examples ​

js
const list = new UICollectionView(
  UICollectionViewCompositionalLayout.layoutWithListConfiguration(
    new UICollectionLayoutListConfiguration('insetGrouped')));
list.addEventListener('beforedisplay', (e) => {
  console.log(e instanceof UICollectionViewItemEvent, e.indexPath.item);
});

Effect: before each item of the list is shown on screen, prints true and its position.

Corresponding iOS API ​

ObjCJS
collectionView:didSelectItemAtIndexPath:itemselect
collectionView:didDeselectItemAtIndexPath:itemdeselect
collectionView:willDisplayCell:forItemAtIndexPath:beforedisplay
collectionView:didEndDisplayingCell:forItemAtIndexPath:displayend
The indexPath parameterevent.indexPath

See also ​