2017-09-01 70 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