UICollectionViewItemEvent
UICollectionViewItemEvent 表示列表里某一项发生的事:被选中、取消选中、要显示、不再显示。
UICollectionView 的四个事件是它,indexPath 是那一项的位置。
继承关系:Event → UICollectionViewItemEvent
构造函数
创建一个 UICollectionViewItemEvent。运行时派发下面这些事件时自己创建它;App 的代码可以用它建一个事件对象,用 dispatchEvent() 派发出去,试一试监听器。
实例属性
还继承 Event 的属性,最常用的是 type 和 target。下表的属性都是只读的,给它们赋值抛 TypeError。
| 属性 | 说明 |
|---|---|
indexPath 只读 | 事件发生那一刻这一项的位置 |
这些属性在原型上,不在事件对象自己身上:Object.keys(event) 和 { ...event } 拿不到它们,要一个一个读。见带字段的事件。
哪些事件是这个类
| 对象 | 事件 |
|---|---|
UICollectionView | itemselect、itemdeselect、beforedisplay、displayend |
不能取消:cancelable 是 false,preventDefault() 不起作用。
示例
js
const list = new UICollectionView(
UICollectionViewCompositionalLayout.layoutWithListConfiguration(
new UICollectionLayoutListConfiguration('insetGrouped')));
list.addEventListener('beforedisplay', (e) => {
console.log(e instanceof UICollectionViewItemEvent, e.indexPath.item);
});效果:列表的每一项要显示到屏幕上之前,输出 true 和它的位置。
对应的 iOS API
| ObjC | JS |
|---|---|
collectionView:didSelectItemAtIndexPath: | itemselect |
collectionView:didDeselectItemAtIndexPath: | itemdeselect |
collectionView:willDisplayCell:forItemAtIndexPath: | beforedisplay |
collectionView:didEndDisplayingCell:forItemAtIndexPath: | displayend |
参数 indexPath | event.indexPath |
参见
UICollectionView:派发这些事件的对象NSIndexPath