4 uitableview uiimagepickercontroller ios swift
我有一个带有单元格的tableView.在这个单元格中,我有一个imageView和一个覆盖它的按钮.该按钮具有IBAction.点击时,该按钮应该召唤imagePicker.IBAction代码位于单元子视图中.
@IBAction func MyStatusImageButtonAction(sender: AnyObject) {
    // Select Image
    let imagePicker = UIImagePickerController()
    imagePicker.delegate = self
    imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
    imagePicker.allowsEditing = false
    self.presentViewController(imagePicker, animated: true, completion: nil)
因为这行我得到一个错误.
self.presentViewController(imagePicker, animated: true, completion: nil)
错误说"类型的值"myCell"没有成员"presentViewController".
我是否应该在托管单元格的viewController中加载imagePicker,然后以某种方式将其传输到单元格?我应该用不同的代码在单元格中加载imagePicker吗?我错了吗?
为清楚起见,我的目标是让用户加载此TableViewController,并且只能将图像分配给此单元格中的imageView.
谢谢.
小智 5
编写协议,TableViewController将为您显示.
protocol ImagePickerDelegate {
    func pickImage()
}
在你的牢房里
var delegate : ImagePickerDelegate?
@IBAction func pickImage(sender: AnyObject) {
    delegate?.pickImage()
}
在你的tableViewController中
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("displayImage", forIndexPath: indexPath) as! ImageTableViewCell
    cell.delegate = self
    return cell
}
func pickImage() {
    let imagePicker = UIImagePickerController()
    imagePicker.delegate = self
    imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
    imagePicker.allowsEditing = false
    self.presentViewController(imagePicker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
    dismissViewControllerAnimated(true, completion: nil)
    var cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0)) as! ImageTableViewCell
    cell.displayImageView.image = image
}
希望这有帮助.
只有一个viewController可以呈现另一个viewController。所以使用:
self.view.presentViewController(imagePicker, animated: true, completion: nil)
| 归档时间: | 
 | 
| 查看次数: | 2423 次 | 
| 最近记录: |