UIImagePickerController(didFinishPickingMediaWithInfo)未在UITableViewCell Swift中调用

时间:2015-03-21 15:44:01

标签: ios uitableview swift uiimagepickercontroller ios8.2

我有一个UITableViewCell按钮来拍摄图像并将其放回到单元格中。当我调用UIImagePickerController并选择/拍摄图像时,它应调用以下函数

 func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject], sender:AnyObject) 

由于某种原因,它没有调用此函数,因此,我无法将所选的UIImage放置到当前单元格。任何帮助都会非常有用。

这是按钮代码

@IBAction func takePhoto(sender: AnyObject) {
    let imagePickerController = UIImagePickerController()
    imagePickerController.delegate = self
    imagePickerController.allowsEditing = true

    let actionSheet = UIAlertController(title: "Choose image souruce", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)

    actionSheet.addAction(UIAlertAction(title: "Take Image", style: UIAlertActionStyle.Default, handler: { (alert:UIAlertAction!) -> Void in
        imagePickerController.sourceType = UIImagePickerControllerSourceType.Camera
        self.presentViewController(imagePickerController, animated: true, completion: nil)
    }))

    actionSheet.addAction(UIAlertAction(title: "Photo Library", style: UIAlertActionStyle.Default, handler: { (alert:UIAlertAction!) -> Void in
        imagePickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        self.presentViewController(imagePickerController, animated: true, completion: nil)
    }))
    actionSheet.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))
    self.presentViewController(actionSheet, animated: true, completion: nil)

}

这是didFinishPickingMediaWithInfo

上的imagePickerController函数

self.reloadData是运行UITableView并重新加载数据的函数。

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject], sender:AnyObject) {
    var btnPos: CGPoint = sender.convertPoint(CGPointZero, toView: self.tableView)
    //Gets the chosen cell indexPath
    var indexPath: NSIndexPath = self.tableView.indexPathForRowAtPoint(btnPos)!
    let currentCell:TableViewCell = tableView.cellForRowAtIndexPath(indexPath) as TableViewCell
    let iamgePicked:UIImage = info[UIImagePickerControllerOriginalImage] as UIImage
    currentCell.cellImageOulet.image = iamgePicked
    picker.dismissViewControllerAnimated(true, completion: nil)
    self.loadData()


}

1 个答案:

答案 0 :(得分:0)

let currentCell:TableViewCell = tableView.cellForRowAtIndexPath(indexPath) as TableViewCell

这不起作用。 Table View具有某种声明式的数据源。 这意味着您需要保存要在表视图中显示的图像,并在tableViewCellForRowAtIndexPath中使用它们