Reputation: 79
I'm trying to build a recipe app with a recipe upload function. In the PostController, there will be a tableview of all the cooking steps, each in a tableview cell. in the cell there will be a textfield of description and a UIImageView for picture upload ( the picture chosen from pickerController will be displayed in this UIImageView for later upload). I'm trying to do
imageViewInCell.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleImageUpload)))
to call thehandleImageUpload()
function that generates a UIImagePickerController. But by doing this, I met two problems.
index.row
value of the cell by the selector in UITapGestureRecognizer
, with out the index I cannot assign the chosen image back to the UIImageView of the cell.Even if I got index.row in handleImageUpload, I still need the function below to assign selected image. How would this function accept my parameter and find the corresponding imageViewInCell?
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let selectedImage: UIImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
imageViewInCell.image = selectedImage
}
dismiss(animated: true, completion: nil)
}
Upvotes: 1
Views: 1569
Reputation: 106
I'm pretty late to the party, but the code below should work.
I'd have the UITableViewCell do the work of dealing with the image. Make it a UIPickerControllerDelegate and have it hold onto a closure to present the picker when necessary (as it can't present something itself as it is not a view controller).
Something like:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let photoCell = tableView.dequeueReusableCell(withIdentifier: "PhotosTableViewCell", for: indexPath) as! PhotosTableViewCell
photoCell.takePicture = {[weak self] in
let imagePicker = UIImagePickerController()
imagePicker.delegate = photoCell
imagePicker.sourceType = .camera
self?.present(imagePicker, animated: true, completion: nil)
}
Have your UITableviewCell implement:
var takePicture: (() -> Void)?
to hold the closure. Then call it from a button or something to make it trigger. Use:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
to deal with the image.
Upvotes: 0
Reputation: 1713
I'm assuming you want thehandleImageUpload()
to be called only on tap on imageView
instead of whole cell, hence you're using tapGesture.
Now to answer your question, assign tag to your imageView like this :
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
//cell configuration
cell.imageView.tag = indexPath.row
return cell
}
And you can assign selected image to selected cell like this:
Now your handleImageUpload()
is:
func handleImageUpload(_ sender: UITapGestureRecognizer){
selectedIndexPath = sender.tag
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let selectedImage: UIImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
let cell = self.tableView.cellForRowAtIndexPath(selectedIndexPath) as UITableViewCell
cell.imageViewInCell.image = selectedImage
}
dismiss(animated: true, completion: nil)
}
Upvotes: 0
Reputation: 1396
you can set the indexPath.row as the tag of your imageview in cellForRowAtIndexPath like Below
cell.yourImageView.tag = indexPath.row
and then you can get this indepath backagain using below
let indexPath = NSIndexPath(forRow: sender.tag, inSection: 0)
let cell = tableView.cellForRowAtIndexPath(indexPath) as! yourcellClass!
cell.yourImgeView.image = selectedImage
Upvotes: 1