2017-09-01 13 views
2

UITableViewCellには、UIImageUIButtonが含まれています。ボタンをタップすると、imagePickerControllerが呼び出され、撮影された画像がUITableViewCell'sUIImageに配置されている機能を実装しようとしています。私はプロトコルの作成に関するチュートリアルを使ってみましたが、それを全く動かすことはできません。open imagePickerController from UITableViewCell

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 
} 

} 

EDIT:

私は今、カメラがレンダリングしているが、私はまだ選ばれた画像にアクセスし、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