open camera from UITableViewCell iOS

孤街醉人 提交于 2019-12-08 05:15:34

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!