问题
I have a UITableView and a UITableViewCell. In every UITableViewCell I want to open gallery or click an image. I normally present UIImageviewController like
- (IBAction)takePhoto:(UIButton *)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
How can I present from cell ?
Misha :)
回答1:
The proper solution is to use proper MVC design. Have your custom cell define a protocol for its own delegate methods. Setup your table view controller to be each cell's delegate.
One of the protocol methods would be a method asking the delegate to present a view controller on its behalf.
Your cell protocol would have a method such as:
- (void)cell:(CustomTableViewCell *)cell presentViewController:(UIViewController *)controller;
Your custom cell's takePhoto
method you do something like this:
- (IBAction)takePhoto:(UIButton *)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self.delegate cell:self presentViewController:picker];
}
Then your table view controller would implement the method:
- (void)cell:(CustomTableViewCell *)cell presentViewController:(UIViewController *)controller {
[self presentViewController:controller animated:YES completion:NULL];
}
来源:https://stackoverflow.com/questions/32829035/open-camera-from-uitableviewcell-ios