【问题标题】:How to pre select cell on UICollectionView如何在 UICollectionView 上预先选择单元格
【发布时间】:2013-10-11 09:01:43
【问题描述】:
我正在做一个项目,我想从“已保存的照片”和名为“myAlbum”的相册中读取图像,并将它们列在 UICollectionView 上,并提供这样的多项选择
http://screencast.com/t/Y8VCgrWbH
当用户单击 myAlbum 相册中的 myAlbum 图像时,当用户单击显示的画廊中的画廊图像时,每次用户单击两个按钮之一时,我都会重新加载 UIcollectionView 数据。我还将选定的图像保存在可变数组中,如果未选中则将其删除。我想将图像与选定的数组进行比较,然后显示已在
中选择的单元格
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
当用户同时浏览两个按钮时。
【问题讨论】:
标签:
iphone
ios
objective-c
uicollectionview
uicollectionviewcell
【解决方案1】:
尝试保存您要选择的单元格,然后调用此委托方法:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
把它放在viewWillAppear 中。如果您已保存数据,您应该能够找出需要选择的索引。
【解决方案2】:
首先,授权你的collection view的多选:
self.collectionView.allowsMultipleSelection = YES;
之后,实现两个委托方法:
#pragma mark – UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
// Update Cell Selection
[_selectedItems addObject:[_data objectAtIndex:indexPath.item]];
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
// Update Cell Selection
[_selectedItems removeObject:[_data objectAtIndex:indexPath.item]];
}
_data 是包含所有对象的数组,_selectedItems 是一个 NSMutableArray,用于保存当前是否选中。
在此处修改单元格的外观:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
最后,想了解更多,可以看here。