Reputation: 205
I want to allow user to select only one cell & data will be reflected based on that. (so only one cell will be selected at a time.) but instead if I click fast with multiple finger I m able to select multiple cell at certain time.
I have done this with didSelectItemAtIndexPath
& didDeselectItemAtIndexPath
as well but in that case I m not able to select.
Can anyone help for this feature?
Here is the sample code that I m using:-
[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];
});
}
Upvotes: 1
Views: 1330
Reputation: 205
Finally, I have found solution.
I just made my own custom didDeselect method & handle in cellForItemAtIndexPath.
Upvotes: 0
Reputation: 1167
following is my working code.
I declared previousIndexPath
in .h like follo
below
@property (nonatomic, strong) NSIndexPath *previousIndexPath;
Now in .m file
-(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];
}
}
I compared indexpath directly using isEqual:
method instead of indexPath.row and == operators.
Upvotes: 2