我发现了几个类似的问题,但没有回答我的问题。我希望用户选择一个表格单元格,打开相机,拍照并将该照片加载到表格单元格中的imageView中。到目前为止,这是我的代码片段。我只是丢失了如何将这张照片添加到表格单元格中。谢谢!
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]){
imagePicker.dismissViewControllerAnimated(true, completion: nil)
var photo = info[UIImagePickerControllerOriginalImage] as? UIImage
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return bedroomCells.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as UITableViewCell
let row = indexPath.row
cell.textLabel?.text = bedroomCells[row]
//cell.imageView?.image =
return cell
}
答案 0 :(得分:2)
试试这个,似乎对我有用:
class ExampleTableViewController: UITableViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
struct TestStruct {
var text: String?
var image: UIImage?
}
var imagePicker = UIImagePickerController()
var bedroomCells = [TestStruct]()
var lastSelectedIndex: NSIndexPath?
override func viewDidLoad() {
super.viewDidLoad()
imagePicker.delegate = self
for var i = 0; i < 10 ; i++ {
var entry = TestStruct(text: "\(i)", image: nil)
bedroomCells.append(entry)
}
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return bedroomCells.count;
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("sampleCell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel?.text = bedroomCells[indexPath.row].text
cell.imageView?.image = bedroomCells[indexPath.row].image
return cell
}
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
self.lastSelectedIndex = indexPath // Save the selected index
self.presentViewController(imagePicker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
imagePicker.dismissViewControllerAnimated(true, completion: nil)
var photo = info[UIImagePickerControllerOriginalImage] as? UIImage
bedroomCells[lastSelectedIndex!.row].image = photo // Set the image for the selected index
tableView.reloadData() // Reload table view
}
}
答案 1 :(得分:0)
尝试使用此示例git代码,我已经解决了使用自定义单元格,PrepareforReuse和willDisplay单元格的常见tableviewCell问题,以显示不使用时图像被重置的情况。 此外,它还包含可重用的cameraAlert,可以从任何视图控制器中调用它,并且可以从图库或摄像机捕获中进行选择。
https://github.com/shruezee/MultipleImage-TableViewCell-CameraCapture