2
我有一個UITableViewCell
,它包含一個UIImage
和一個UIButton
。我試圖實現功能,當按鈕被輕敲時,imagePickerController
被調用並且拍攝的照片被放置在那個UITableViewCell's
UIImage
中。我嘗試使用關於創建一個協議的教程,但我似乎無法讓它工作。從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)
}
添加這個閉包是做什麼的?對不起,如果這是一個明顯的,我以前沒有見過。 – mikew
當你調用你的閉包()時,你正在傳遞一個代碼塊作爲變量,然後執行這個代碼塊 –
那麼我可以在我的UITableViewCell文件的tappedCamera函數中使用UIImagePickerController嗎? – mikew