我想让用户只选择一个小区&数据将基于此反映出来。 (因此,一次只能选择一个单元格。)但是如果我用多个手指快速点击,我可以在特定时间选择多个单元格。
我已经使用didSelectItemAtIndexPath
& didDeselectItemAtIndexPath
也是{但是在那种情况下我无法选择。
任何人都可以为此功能提供帮助吗?
以下是我使用的示例代码: -
[CustomCollection setShowsHorizontalScrollIndicator:NO];
[CustomCollection setBounces:NO];
[CustomCollection setAllowsMultipleSelection:NO];
[CustomCollection setMultipleTouchEnabled:NO];
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"TheCustomCell";
CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
if (previousIndexPath.row == indexPath.row) {
// Code for Selecttion
[cell.tickImgView setImage:[UIImage imageNamed:@"Select_tick"]];
}
else{
[cell.tickImgView setImage:[UIImage imageNamed:@"Deselect_tick"]];
}
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
dispatch_async(dispatch_get_main_queue(), ^{
previousIndexPath = indexPath;
[CustomCollection reloadData];
});
}
答案 0 :(得分:1)
以下是我的工作代码。
我在.h中声明了previousIndexPath
以下
@property (nonatomic, strong) NSIndexPath *previousIndexPath;
现在在.m文件
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"TheCustomCell";
CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
if ([self.previousIndexPath isEqual:indexPath]) {
// Code for Selecttion
[cell.tickImgView setImage:[UIImage imageNamed:@"Select_tick"]];
}
else{
//Code for deselect
[cell.tickImgView setImage:[UIImage imageNamed:@"Deselect_tick"]];
}
return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//reload collectionview data only if its not selected already
if (![self.previousIndexPath isEqual:indexPath])
{
self.previousIndexPath = indexPath;
[collectionView reloadData];
}
}
我使用isEqual:
方法而不是indexPath.row和==运算符直接比较了indexpath。
答案 1 :(得分:0)
最后,我找到了解决方案。
我刚刚制作了自己的自定义didDeselect方法&处理cellForItemAtIndexPath。