0

如何設置UICollectionViewCell作爲按鈕使用,該功能完成後也會重複使用。 我想用單元格作爲按鈕來添加來自我的照片庫的圖像。 代碼提取是我用來添加圖像的barbutton項目的功能。使用UICollectionViewCell作爲按鈕

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

我的代碼提取物是從我ViewController.m文件,我做了一個cellbutton模型類。如何從那裏得到它 – 000

回答

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


} 
2

您可以實現此功能:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
// something you do as function "buttonTapped" 
} 
+0

還要確保爲「按鈕」所在的indexPath包含一個「if()」或「switch()」。 – Scott