Collectionview单选
Collectionview single selection
我想允许用户 select 只有一个单元格和数据将基于它反映出来。 (因此一次只会 select 编辑一个单元格。)但是如果我用多个手指快速单击,我可以在特定时间 select 多个单元格。
我也用 didSelectItemAtIndexPath
和 didDeselectItemAtIndexPath
完成了此操作,但在那种情况下我无法 select。
任何人都可以为此功能提供帮助吗?
这是我使用的示例代码:-
[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];
});
}
以下是我的工作代码。
我在 .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 和 == 运算符比较了索引路径。
终于找到解决方法了
我刚刚在 cellForItemAtIndexPath 中制作了自己的自定义 didDeselect 方法和句柄。
我想允许用户 select 只有一个单元格和数据将基于它反映出来。 (因此一次只会 select 编辑一个单元格。)但是如果我用多个手指快速单击,我可以在特定时间 select 多个单元格。
我也用 didSelectItemAtIndexPath
和 didDeselectItemAtIndexPath
完成了此操作,但在那种情况下我无法 select。
任何人都可以为此功能提供帮助吗?
这是我使用的示例代码:-
[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];
});
}
以下是我的工作代码。
我在 .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 和 == 运算符比较了索引路径。
终于找到解决方法了
我刚刚在 cellForItemAtIndexPath 中制作了自己的自定义 didDeselect 方法和句柄。