2017-02-07 8 views
0

私はSwift 3、Xcode 8.2を使用しています。私はカスタムビュービューセル(MyCustomCellと呼ばれる)を持っています。そこには、画像ビューとクリックされたときにカメラを開くボタンがあります。ユーザーはこれらのセルをさらに追加できます。ユーザーがボタンをクリックして写真を撮って、そのボタンの適切な画像表示に表示させたいと思っています。iOS - 選択した画像を適切な画像ビュー(imagePickerController)に配置する方法

私の問題は、今は常に最初の画像ビューに表示されていますが、他の画像ビューには表示されないということです。

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が呼び出される方法を理解するに苦労し、私は、ユーザーがクリックしたカスタムセルに渡すことができるかどうかです。

は、私はこのような何かやりたい:cellは何とか引数に渡され

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

を私はimagePickerControllerの署名を変更することができるかどうかわからないんだけどもしそうなら、それをどのように正確ですと呼ばれる?

ご協力いただければ幸いです。ありがとう!

答えて

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

これが役に立ちます。

+0

私はこのようにはしませんでしたが、この回答は正しい軌道に乗るのを助けました。ありがとうございました! – noblerare

0

あなたのMyCustomCellクラスでは、ユーザーが選択した写真を1つの写真を選択して返すことができるimagePickerのこの機能が必要です。次に、cellViewRowを使用したtableViewControllerデータソースメソッド(セルのindexPathも渡します)ユーザーは、あなたが

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

    return cell 
} 
関連する問題