选定单元格 UICollectionView 的索引路径
IndexPath for Selected Cell UICollectionView
我目前正在研究显示各种单元格的 UICollectionView ...
到目前为止一切顺利,但我对细胞的 selection 有问题...例如,我实现了 selection 的方法,如下所示:
- (Void)collectionView:(UICollectionView *) collectionView didSelectItemAtIndexPath:(NSIndexPath *) indexPath {
UICollectionViewCell theCell * = (UICollectionViewCell *) [CollectionView cellForItemAtIndexPath: indexPath];
theCell.backgroundColor = [UIColor orangeColor];
}
问题是,当我浏览集合视图时,selected 单元格除了生成橙色外还会生成其他 selection ..
示例:select 单元格编号 7 并且自动 select 单元格编号 14 ...显然我不需要这个,但我希望它保持 selected只有单元格编号 7
我哪里做错了?我的 indexPath 有问题吗?
在方法中
(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
像这样添加某物:
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"yourCelId" forIndexPath:indexPath];
if (cell.selected) {
cell.backgroundColor = [UIColor orangeColor];
} else {
cell.backgroundColor = [UIColor clearColor];
}
这是因为单元格被重复使用,您必须重置所有可以更改的值。
我目前正在研究显示各种单元格的 UICollectionView ...
到目前为止一切顺利,但我对细胞的 selection 有问题...例如,我实现了 selection 的方法,如下所示:
- (Void)collectionView:(UICollectionView *) collectionView didSelectItemAtIndexPath:(NSIndexPath *) indexPath {
UICollectionViewCell theCell * = (UICollectionViewCell *) [CollectionView cellForItemAtIndexPath: indexPath];
theCell.backgroundColor = [UIColor orangeColor];
}
问题是,当我浏览集合视图时,selected 单元格除了生成橙色外还会生成其他 selection ..
示例:select 单元格编号 7 并且自动 select 单元格编号 14 ...显然我不需要这个,但我希望它保持 selected只有单元格编号 7
我哪里做错了?我的 indexPath 有问题吗?
在方法中
(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
像这样添加某物:
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"yourCelId" forIndexPath:indexPath];
if (cell.selected) {
cell.backgroundColor = [UIColor orangeColor];
} else {
cell.backgroundColor = [UIColor clearColor];
}
这是因为单元格被重复使用,您必须重置所有可以更改的值。