Mihir Oza
Mihir Oza

Reputation: 2796

Selection and unselection isselection issue in UICollectionView

I am facing the same issue Multiple selections Issue

Here is my code which I have done.

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    RCCollectionCell* cell = [_channelCollectionView dequeueReusableCellWithReuseIdentifier:@"RC" forIndexPath: indexPath];
    cell.layer.cornerRadius = 5.0f;
    cell.layer.borderWidth=1.0f;
    cell.layer.borderColor=[UIColor lightGrayColor].CGColor;
    if (indexPath.row < [_fC count]){
        [cell setChannel:_favoriteChannels[indexPath.row]];
        [_channelCollectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
    } else {
        //NSLog(@"Error cell %d requested only %d channels in incident", (int)indexPath.row, (int)[_incident.channels count]);
    }
    return cell;
}  
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    RCCollectionCell* cell = [_channelCollectionView dequeueReusableCellWithReuseIdentifier:@"RC" forIndexPath: indexPath];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    RCCollectionCell* cell = (RCCollectionCell*)[collectionView cellForItemAtIndexPath:indexPath];
    Channel* channel = [self getChannelForIndexPath:indexPath];
    if ([_upload.channels containsObject:channel.uuid]) {
        [_upload.channels removeObject:channel.uuid];
        cell.selectedImage.hidden = YES;

    } else {
        [self.view makeToast:channel.name duration:1.5 position:CSToastPositionCenter];

        [_upload.channels addObject:channel.uuid];
        cell.selectedImage.hidden = NO;
    }
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
}  

My Problem is cell.selectedImage.hidden = NO; when I click on any cell and when I scroll the collectionview I can see that another cell is also affected with selectedimage.hidden = no.

Please suggest me some solutions to resolve this issue.

Thanks in advance.

EDIT:

selectedImage is a checkmark image which I am using to check and uncheck the cell.

Upvotes: 1

Views: 69

Answers (1)

Vinu Jacob
Vinu Jacob

Reputation: 381

First you create a NSMutableArray to store the selected collectionview indexpaths.

NSMutableArray *SelectedIndexes = [[NSMutableArray alloc]init];

and add the indexpath in the if conditoin of didselect method

[SelectedIndexes addObject:indexPath];

And remove the indexpath inside "else" condition of didselect method.

if ([SelectedIndexes containsObject:indexPath]) {
    [SelectedIndexes removeObject:indexPath];
}

In your cellForItemAtIndexPath method check for the selected indexpath.

if ([SelectedIndexes containsObject:indexPath]) {
   cell.selectedImage.hidden = YES;
}
else {
    cell.selectedImage.hidden = NO;
}

Upvotes: 1

Related Questions