2

I'm working with UICollectionView using PSTCollectionView Library. I've to create a grid where user can select and deselect images by tapping on UICollectionViewCell. I've to show checkBox like image if cell is selected. And uncheckedBox image if cell is deselected. I am able to select cell and display checkBox image.And also can deselect. But when I select next cell, the previous deselected cell also get selected and shows checkBox image. This is the method I declared in UICollectionViewCell subClass

 -(void)applySelection{
    if(_isSelected){
        _isSelected=FALSE;
        self.contentView.backgroundColor=[UIColor whiteColor];
        self.selectImage.image=[UIImage imageNamed:@"unchecked_edit_image.png"];
    }else{
        _isSelected=TRUE;
        self.contentView.backgroundColor=[UIColor whiteColor];
        self.selectImage.image=[UIImage imageNamed:@"checked_edit_image.png"];
    }
}

And here is my code for didSelectItemAtIndexPath and didDeselectItemAtIndexPath

- (void)collectionView:(PSTCollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"didSelect method called");
    FriendImageCell *cell = (FriendImageCell*)[imageGrid cellForItemAtIndexPath:indexPath];
        [selectedImages addObject:[[list objectAtIndex:indexPath.item] objectForKey:@"thumbnail_path_150_150"]];
         [cell applySelection];

}

- (void)collectionView:(PSTCollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"did deselect called");
    FriendImageCell *cell = (FriendImageCell*)[imageGrid cellForItemAtIndexPath:indexPath];
    [selectedImages removeObjectAtIndex:indexPath.item];
    [cell setSelected:NO];
    [cell applySelection];
}

Can anyone please make me understand whats wrong with my code ? Make me correct if I'm doing anything wrong. Tried many answers on stack-overflow but nothing worked. Any help would be appreciated. Thanks in advance.

n00bProgrammer
  • 4,261
  • 3
  • 32
  • 60
Sushil Sharma
  • 2,321
  • 3
  • 29
  • 49
  • What is your `indexPath.item`? I cannot find it in the [NSIndexPath reference](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSIndexPath_Class/). – Yuchen Jan 17 '15 at 23:31
  • In `UICollectionView`, cell is considered as item. – Sushil Sharma Jan 19 '15 at 07:37

1 Answers1

1

After days of back and forth discussion. I think I finally understand what your problem really is. You must have forgotten to set the allowsMultipleSelection to YES. So whenever a new cell is selected, your previous cells got deselected.

allowsMultipleSelection

This property controls whether multiple items can be selected simultaneously. The default value of this property is NO.

And in my previous answer, I also suggested you to make your own boolean array to keep track of the selected items. However, I just realized that you don't have to. indexPathsForSelectedItems gives you an array of selected index paths.

indexPathsForSelectedItems

An array of NSIndexPath objects, each of which corresponds to a single selected item. If there are no selected items, this method returns an empty array.

As a matter of fact, you don't even have to implement the didSelectItemAtIndexPath, and didDeselectItemAtIndexPath. By default, these two delegate methods will call setSelected: for you. Therefore, a more appropriate way to do is move your applySelection code in to setSelected.

Overwrite the setSelected: method in your custom UICollectionViewCell.

- (void)setSelected:(BOOL)selected
{
    [super setSelected:selected];

    // Change your UI
    if(_isSelected){
        self.contentView.backgroundColor=[UIColor whiteColor];
        self.selectImage.image=[UIImage imageNamed:@"unchecked_edit_image.png"];
    }else{
        self.contentView.backgroundColor=[UIColor whiteColor];
        self.selectImage.image=[UIImage imageNamed:@"checked_edit_image.png"];
    }
}
Yuchen
  • 30,852
  • 26
  • 164
  • 234
  • How to create a array of boolean? – Sushil Sharma Jan 19 '15 at 07:28
  • Yay, I just want to say it is a array of boolean. Let me just add "How to create a array of boolean?" to the answer, its a bit length to explain in comments.. – Yuchen Jan 19 '15 at 13:52
  • Or maybe you can refer to this post: http://stackoverflow.com/questions/628343/objective-c-boolean-array – Yuchen Jan 19 '15 at 13:54
  • Thanks for the answer but it doesn't allow multiple cell selection. – Sushil Sharma Jan 20 '15 at 05:20
  • It does, that's what we use for our app – Yuchen Jan 20 '15 at 12:32
  • I understand, But the problem is When I select a cell ,then select next cell,It deselects the previous one and image gets changed. – Sushil Sharma Jan 22 '15 at 06:20
  • Yes, of cause a previous cell will get deselected. That why u cannot rely on cell default selected property for ur purposes. You on need to define your own datasource. An array of Boolean. U not even trying are u? – Yuchen Jan 22 '15 at 12:40
  • @Sushil, sorry that i may have led you the wrong way in my previous answer. I made a huge modification to it. This should adjust your issue now. – Yuchen Jan 24 '15 at 17:26
  • Thanks @yuchen, Actually I was using wrong variable of PSUICollectionView to allowMultipleSelection. Choosing the right variable solved the issue. Thanks. – Sushil Sharma Jan 30 '15 at 10:59