2016-11-30 35 views
1

我在尝试使用配方上传功能来构建食谱应用程序。在PostController中,所有烹饪步骤将有一个桌面视图,每个烹饪步骤都在桌面视图单元中。在单元格中将会有一个文本域的描述和一个UIImageView用于图片上传(从pickerController中选择的图片将在这个UIImageView中显示以供以后上传)。我正在尝试执行 imageViewInCell.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleImageUpload))) 以调用生成UIImagePickerController的handleImageUpload()函数。但是通过这样做,我遇到了两个问题。在每个tableview单元格中使用UIImagePickerController

  1. 我不能在UITapGestureRecognizer由选择器获得的小区的index.row值,与列的索引我不能选择的图像分配返回到信元的的UIImageView。
  2. 即使我在handleImageUpload中获得了index.row,我仍然需要下面的函数来分配选定的图像。这个函数如何接受我的参数并找到相应的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) 
    } 
    
+1

为什么使用轻击手势而不是didSelectRowAtIndexPath? –

+0

我为imageView添加了UITapGesture,因为此操作仅仅与图片相关。使用didSelectRowAtIndexPath可能不是选择,因为单元格中还有一个文本字段。 –

+0

最后,我每次调用'handleImageUpload()'时都保存了全局变量'selectedIndex'(由didFinishPickingMediaWithInfo'使用)和'cell.imageViewInCell.tag'(由'handleImageUpload()'使用)的索引,它赋值'cell.imageViewInCell.tag'的'selectedIndex'。在'didFinishPickingMediaWithInfo'中,它使用全局变量来查找相应的单元格。 –

回答

1

可以设置indexPath.row在你的cellForRowAtIndexPath的imageview的像下面

cell.yourImageView.tag = indexPath.row 

标签,然后你可以用下面

let indexPath = NSIndexPath(forRow: sender.tag, inSection: 0) 
let cell = tableView.cellForRowAtIndexPath(indexPath) as! yourcellClass! 
cell.yourImgeView.image = selectedImage 
+0

这个想法有帮助。问题现在已经解决了,谢谢! –

+0

欢迎开心编码.... –

0
得到这个indepath backagain

我假设你只想在imageView上调用thehandleImageUpload()而不是整个单元,因此您使用tapGesture

现在回答你的问题,分配标签,就可以ImageView的是这样的:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
{ 
//cell configuration 
cell.imageView.tag = indexPath.row 
return cell 
} 

而且,您可以选择图像分配给选定的单元格是这样的:

现在你handleImageUpload()是:

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) 
} 
+0

使用手势方法,您只能传递GestureRecognizer对象而不是其他任何东西。 –

+0

好的,让我编辑一下。 –

+0

编辑答案。 –

相关问题