2017-09-01 69 views
2

我有一个UITableViewCell,它包含一个UIImage和一个UIButton。我试图实现功能,当按钮被轻敲时,imagePickerController被调用并且拍摄的照片被放置在那个UITableViewCell'sUIImage中。我尝试使用关于创建一个协议的教程,但我似乎无法让它工作。从UITableViewCell中的按钮打开imagePickerController

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

回答

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 
} 
+0

添加这个闭包是做什么的?对不起,如果这是一个明显的,我以前没有见过。 – mikew

+0

当你调用你的闭包()时,你正在传递一个代码块作为变量,然后执行这个代码块 –

+0

那么我可以在我的UITableViewCell文件的tappedCamera函数中使用UIImagePickerController吗? – mikew