【问题标题】:iOS - How to place chosen images onto the appropriate image view (imagePickerController)iOS - 如何将选择的图像放置到适当的图像视图 (imagePickerController)
【发布时间】:2017-02-07 03:56:23
【问题描述】:

我正在使用 Swift 3、Xcode 8.2。我有一个自定义表格视图单元(称为 MyCustomCell),其中有一个图像视图和一个按钮,单击该按钮可打开相机。用户可以添加更多这些单元格。我希望用户能够点击一个按钮、拍照,然后将其显示在该按钮的相应图像视图中。

我的问题是,现在它总是出现在第一个图像视图中,而不是其他图像视图中。

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

我很难理解 imagePickerController 是如何被调用的,以及我是否可以传入用户单击的自定义单元格。

我想做这样的事情:

tableView.cellForRow(at: tableView.indexPath(for: cell)) 

cell 以某种方式传递到参数中,但我不确定imagePickerController 的签名是否可以修改,如果可以,它是如何调用的?

任何帮助将不胜感激。谢谢!

【问题讨论】:

    标签: ios swift xcode swift3 uiimagepickercontroller


    【解决方案1】:

    在 cellForRow 方法中为按钮添加标签,并使用该标签获取单元格。

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

    希望这会有所帮助。

    【讨论】:

    • 我并没有这样做,但这个答案帮助我走上了正轨。谢谢!
    【解决方案2】:

    在您的 MyCustomCell 类中,您应该为 imagePicker 提供此功能,用户可以在其中选择一张照片并返回用户选择的照片。然后,在 tableViewController 数据源方法中使用 cellForRow (您还可以将单元格的 indexPath 传递给您的用户点击)你应该有类似

     func tableView_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
         let cell = tableView.dequeueReusableCell(...) as! MyCustomCell
         let image = cell.pickImage
         cell.image = image
    
         return cell 
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-31
      • 2011-08-21
      • 1970-01-01
      • 2020-12-10
      • 1970-01-01
      • 2019-11-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多