0

I m using collectionCell to select and deselect image on collectionCell. But when i click on selected cell , it wont get deselected.

I changed my Code as per as Nirav Suggestion and it working for current View but when i m coming from another view by passing some object then those object should be marked checked. If i click on mark objects that is selected it does not deselect the cell.

My Code

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

BICollectionCell *cell = (BICollectionCell *)[self.collectionView dequeueReusableCellWithReuseIdentifier:@"BICollectionCell" forIndexPath:indexPath];

CustVehiclesList *objCustVehiclesList =self.array_VehicleServicesList[indexPath.row];
[cell.labelMake setText:objCustVehiclesList.make];
[cell.lblLicense setText:objCustVehiclesList.licencePlateNo];

if (objCustVehiclesList.vehiclePicture == nil ||[objCustVehiclesList.vehiclePicture isEqualToString:@""])
{
    [cell.imageCarsView setImage:[UIImage imageNamed:@"placeholder.png"]];
}
else
{
    NSString *baseString = [NSString stringWithFormat:@"%@",objCustVehiclesList.vehiclePicture];
    NSData* imageData = [[NSData alloc] initWithBase64EncodedString:baseString options:0];
    UIImage *imageToDisplay = [UIImage imageWithData:imageData];
    [cell.imageCarsView setImage:imageToDisplay];
}

[cell setSelected:YES];
[self.collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];

if (self.selectedIndexPath == indexPath)
{
    [cell.imgSelectedImage setImage:[UIImage imageNamed:@"vs_tick.png"]];
}
else
{
    [cell.imgSelectedImage setImage:nil];
}

if ([objCustVehiclesList.modelName isEqualToString:self.str_ModelName] && _isCalledFromDetailVC)
{
    [cell.imgSelectedImage setImage:[UIImage imageNamed:@"vs_tick.png"]];
}


if (indexPath.row == self.indexPathToBeSearch.row && self.isCalledFromVehicleVC)
{
    [cell.imgSelectedImage setImage:[UIImage imageNamed:@"vs_tick.png"]];
}


return cell;
}
Nirav D
  • 71,513
  • 12
  • 161
  • 183
ChenSmile
  • 3,401
  • 4
  • 39
  • 69
  • Does `BICollectionCell` overrides `setSelected:` or `setSelected:animated:`. If yes, do you call `[super setSelected:selected]` or `[super setSelected:selected animated:animated]` in it? See http://stackoverflow.com/questions/15330844/uicollectionview-select-and-deselect-issue – Larme Jul 20 '16 at 13:46
  • @Larme i am not overriding any thing in bicollection cell then also same exists – ChenSmile Jul 20 '16 at 13:51
  • 2
    Possible duplicate of [UICollectionView Enable deselecting cells while allowsMultipleSelection is disabled](http://stackoverflow.com/questions/32423778/uicollectionview-enable-deselecting-cells-while-allowsmultipleselection-is-disab) – fishinear Jul 20 '16 at 17:00
  • Add whole code of `cellForRowAtIndexPath` – Nirav D Jul 21 '16 at 08:45
  • @Nirav please check – ChenSmile Jul 21 '16 at 08:48
  • @Nirav same issue exists bro i have placed ur code and checked then also – ChenSmile Jul 21 '16 at 09:52
  • @Imran `self.isCalledFromVehicleVC` is what type of variable? Is it boolean or something else? – Nirav D Jul 21 '16 at 10:43
  • @Nirav self.isCalledFromVehicleVC is a BOOL – ChenSmile Jul 21 '16 at 10:44

1 Answers1

3

If you want to change image on selection of cell and if cell is already selected and you want to deselect it, then you can change your code like this

First Create one instance property selectedIndexPath like this

@property NSIndexPath *selectedIndexPath;

After that change your cellForItemAtIndexPath like this

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    BICollectionCell *cell = (BICollectionCell *)[self.collectionView dequeueReusableCellWithReuseIdentifier:@"BICollectionCell" forIndexPath:indexPath];
    [cell setSelected:YES];
    [self.collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
    if (self.selectedIndexPath == indexPath || ([objCustVehiclesList.modelName isEqualToString:self.str_ModelName] && _isCalledFromDetailVC) || (indexPath.row == self.indexPathToBeSearch.row && self.isCalledFromVehicleVC)) {
        [cell.imgSelectedImage setImage:[UIImage imageNamed:@"vs_tick.png"]];
    }
    else {
        [cell.imgSelectedImage setImage:nil];
    }
    <----Label Values--->
    return Cell;
}

Now in didSelectItemAtIndexPath check for already selected cell like this

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.isCalledFromVehicleVC || self.isCalledFromDetailVC)
    {
     self.isCalledFromVehicleVC = NO;
     self.isCalledFromDetailVC = NO;
    }
    if (self.selectedIndexPath == indexPath) {
        [collectionView deselectItemAtIndexPath:indexPath animated:YES];
        self.selectedIndexPath = nil;
    }
    else {
        self.selectedIndexPath = indexPath;
    }        
    [self.collectionView reloadData];
}

Note - Remove your didDeselectItemAtIndexPath method there is no need of this now.

ChenSmile
  • 3,401
  • 4
  • 39
  • 69
Nirav D
  • 71,513
  • 12
  • 161
  • 183