我发现了几个相似的问题,但没有人回答我的问题。我希望用户选择一个表格单元格,打开相机,拍照,并将照片加载到表格单元格的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
}
最佳答案
试试这个,似乎对我有用:
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
}
}
关于ios - 在表格 View 中显示从相机拍摄的照片,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29440195/