I've been trying to add a checkmark on the collection view's cell when it is tapped by the user, and if it's tapped again - remove it.
So far, I've written a code that adds the checkmark, but still I can't find a way to remove it.
My code:
- (void)profilePicked:(UIGestureRecognizer*)gesture {
UIView *pickedView = (UIView* )[gesture view];
UIImageView *tick = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ticker"]];
tick.frame = CGRectMake(0, 0, 110, 110);
tick.tag = 105;
tick.contentMode = UIViewContentModeScaleAspectFit;
if(![pickedView.subviews containsObject:tick]) {
[pickedView addSubview:tick];
} else {
for (UIView *subview in pickedView.subviews) {
if (subview.tag == 105) {
[subview removeFromSuperview];
}
} [self.profilesCollection reloadData];
}
}
and :
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"profileCell";
InviterCollectionViewCell *cell = (InviterCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
// some stuff happening here
UIView *circle = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 110, 110)];
//cell.profilePicture.frame = CGRectMake(0, 0, 110, 110);
//cell.profilePicture.contentMode = UIViewContentModeScaleAspectFill;
[circle addSubview:imageView];
// Round the edges of the profile picture
circle.layer.cornerRadius = 55;
circle.clipsToBounds = YES;
[cell.contentView addSubview:circle];
cell.ticker.hidden = YES;
//Add Gesture Recognizer for the profile Picture
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(profilePicked:)];
tapped.numberOfTapsRequired = 1;
[circle addGestureRecognizer:tapped];
circle.userInteractionEnabled = YES;
return cell;
}
I tried to implement some functionality in didSelectItemAtIndexPath and didDeselectItemAtIndexPath, but it didn't work as it should.
Any help would be greatly appreciated.
Try this solution :
1 Add UIImageView, with checkmark Image, to your cell in storyboard or nib file then create the outlet for this imageView and set it to hidden mode
2 In your didSelectItemIndexPath method add this code :
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
MyCollectionViewCell *cell = (MyCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
//check if the the checkmark image is hidden then change it to visible
if(cell.checkMarkImage.hidden)
cell.checkMarkImage.hidden = NO;
else
cell.checkMarkImage.hidden = YES;
[cell setSelected:YES];
}