I am using Swift 3, Xcode 8.2. I have a custom Table View Cell (called MyCustomCell) in which there is an image view and a button that, when clicked, opens the camera. The user can add more of these cells. I want the user to be able to click on a button, take a picture, and have that appear in the appropriate image view for that button.
My issue is that right now it is always appearing in the first image view but not the others.
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let pickedImage : UIImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
let scancell = tableView.cellForRow(at: NSIndexPath(row: 0, section: 0) as IndexPath) as! MyCustomCell // it's this line that is wrong
scancell.imgView.contentMode = .scaleAspectFit
scancell.imgView.image = pickedImage
scancell.cameraButton.isHidden = true
};
self.dismiss(animated: true, completion: nil)
}
I'm having hard time understanding how the imagePickerController gets called and if I can pass in the custom cell that the user clicked on.
I want to do something like this:
tableView.cellForRow(at: tableView.indexPath(for: cell))
where cell
is passed into the argument somehow but I'm not sure if the signature of the imagePickerController
can be modified and if so, how exactly is it called?
Any help would be greatly appreciated. Thanks!
Add a tag to the button in cellForRow method and use that tag to get the cell.
var Celltag = 0
func tableView_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell((withIdentifier: "")) as! MyCustomCell
cell.customButton.tag = indexPath.row
cell.customButton.addTarget(self, action: #selector(ButtonClickMethod), for: .touchUpInside)
return cell
}
func ButtonClickMethod (sender:UIButton) {
Celltag = sender.tag
........
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let pickedImage : UIImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
let scancell = tableView.cellForRow(at: NSIndexPath(row: Celltag, section: 0) as IndexPath) as! MyCustomCell
scancell.imgView.contentMode = .scaleAspectFit
scancell.imgView.image = pickedImage
scancell.cameraButton.isHidden = true
};
self.dismiss(animated: true, completion: nil)
}
Hope this helps.