Search code examples
iosobjective-cuicollectionviewuicollectionviewcellibaction

Using a UICollectionViewCell as an Button


how do I set a UICollectionViewCell to be used as a button , that also repeats itself when the function is done. I want to use the cell as a button to add images from my photo library. The code extract is the function of the barbutton item I used to add images.

- (IBAction)buttonTapped:(id)sender {
_picker = [[UIImagePickerController alloc] init];
_picker.delegate = self;
_picker.allowsEditing = NO;
_picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:_picker animated:YES completion:nil];
}



 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
[_images addObject:image];
[self saveNewImageToDb:image];
[self dismissViewControllerAnimated:_picker completion:^{
    [_collectionView reloadData];
}];
}




-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:_picker completion:nil];
}

Solution

  • - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
        NSLog(@"Selected cell = %d",indexPath.item);
    
        _picker = [[UIImagePickerController alloc] init];
        _picker.delegate = self;
        _picker.allowsEditing = NO;
        _picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:_picker animated:YES completion:nil];
    
    
    }