从UITableViewCell中的按钮打开imagePickerController

时间:2017-09-01 03:01:46

标签: ios swift uitableview

我的UITableViewCell包含UIImageUIButton。我正在尝试实现功能,当按下按钮时,调用imagePickerController并将拍摄的照片放在UITableViewCell's UIImage中。我尝试使用有关创建协议的教程,但我似乎无法让它工作。

import UIKit

class CustomTableViewCell: UITableViewCell {

    @IBOutlet weak var imageFound: UIImageView!

    @IBOutlet weak var buttonOutlet: UIButton!

    @IBAction func tappedCamera(_ sender: UIButton) {
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }   
}

这是我的UIViewController代码:

import UIKit

class SoloPlayListViewController: UIViewController, UITableViewDelegate, 
UITableViewDataSource, UIImagePickerControllerDelegate {

var objects = ["cup", "phone", "shoe", "tv"]
var imagePicker : UIImagePickerController?
@IBOutlet weak var tableView: UITableView!
var tempImage : UIImage?

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    imagePicker = UIImagePickerController()

}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return objects.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = Bundle.main.loadNibNamed("CustomTableViewCell", owner: self, options: nil)?.first as! CustomTableViewCell

    return cell
}

}

编辑:

我现在有相机渲染,但我仍然在努力解决如何访问所选图像并将其分配给我单元格中的UIImageView。我尝试了以下方法,包括尝试重新加载数据:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -
> UITableViewCell {
    let cell = Bundle.main.loadNibNamed("CustomTableViewCell", owner: self, 
options: nil)?.first as! CustomTableViewCell

    cell.buttonTouchedClosure = { [weak self] in

            self?.imagePicker!.sourceType = .camera
            self?.present((self?.imagePicker)!, animated: true, completion: nil)
    }

    cell.imageFound.image = tempImage

    return cell
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {

            tempImage = pickedImage
            self.tableView.reloadData()

        }
        dismiss(animated: true, completion: nil)
    }

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    dismiss(animated: true, completion: nil)
}

1 个答案:

答案 0 :(得分:2)

您可以使用闭包进行按钮操作,并在tappedCamera IBAction中调用此闭包

import UIKit

class CustomTableViewCell: UITableViewCell {

    @IBOutlet weak var imageFound: UIImageView!

    @IBOutlet weak var buttonOutlet: UIButton!

    var buttonTouchedClosure : (()->Void)? //closure

    @IBAction func tappedCamera(_ sender: UIButton) {
        self.buttonTouchedClosure?() //closure execution
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }   
}

使用,然后在你的cellForRow方法中你必须设置你的闭包

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = Bundle.main.loadNibNamed("CustomTableViewCell", owner: self, options: nil)?.first as! CustomTableViewCell
    cell.buttonTouchedClosure = { [weak self] in
       debugPrint("Button Touched")
       //Present here your ImagePickerViewController
    }
    return cell
}