如何在 tableViewCell 中运行 imagePicker [英] How to run imagePicker in tableViewCell

查看:17
本文介绍了如何在 tableViewCell 中运行 imagePicker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有单元格的 tableView.在这个单元格中,我有一个 imageView 和一个覆盖它的按钮.该按钮有一个 IBAction.当点击时,按钮应该召唤 imagePicker.IBAction 代码位于单元格子视图中.

I have a tableView with a cell. In this cell I have an imageView, and a button covering it. The button has an IBAction. When tapped, the button is supposed to summon the imagePicker. The IBAction code is located in the cell subview.

@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)

由于这一行,我收到错误消息.

I get an error because of this line.

self.presentViewController(imagePicker, animated: true, completion: nil)

错误提示类型myCell"的值没有成员presentViewController".

Error says "Value of type "myCell" has no member "presentViewController".

我是否应该在托管单元格的 viewController 中加载 imagePicker,然后以某种方式将其传输到单元格?我应该使用不同的代码在单元格中加载 imagePicker 吗?我的意思是不是全错了?

Am I supposed to load the imagePicker in the viewController that hosts the cell, then transfer it to the cell somehow? Am I supposed to load the imagePicker in the cell with different code? Am I going about this all wrong?

为了清楚起见,我的目标是让用户加载这个 TableViewController,并且能够仅将图像分配给该单元格中的 imageView.

For clarity, my goal is for the user to load this TableViewController, and have the ability to assign an image to the imageView in this cell only.

谢谢.

推荐答案

编写一个协议,你的 TableViewController 会为你展示它.

Write a protocol and your TableViewController will show it for you.

protocol ImagePickerDelegate {

    func pickImage()
}

在您的单元格中

var delegate : ImagePickerDelegate?

@IBAction func pickImage(sender: AnyObject) {

    delegate?.pickImage()
}

在你的 tableViewController

In your 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
}

希望对您有所帮助.

这篇关于如何在 tableViewCell 中运行 imagePicker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆