使用 UICollectionViewCell 作为按钮

Using a UICollectionViewCell as an Button

如何将 UICollectionViewCell 设置为用作按钮,该按钮在功能完成时也会自行重复。 我想将单元格用作按钮来从我的照片库中添加图像。 代码摘录是我用来添加图片的barbutton项的功能。

- (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];
}

你可以实现这个功能:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
 // something you do as function "buttonTapped"
 }
- (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];


}