2016-11-22 6 views
0

ViewControllerTableViewがあります。 TableViewCellにはいくつかの内容のtextViewがあります。今すぐcellreplyButtonをクリックした後、seguesecondViewControllerをクリックします。 celltextViewにコンテンツを配置する必要があります。どうやってするの?私はindexPathを以下のコードとして使用しましたが、indexPathnilであるため、クラッシュしました。助けてくれてありがとう。UITableviewCellのボタンをクリックした後に値を転送する方法は?

明確化:didSelectRowAtIndexPath関数を使用するのではなく、目標を実現するためにセル内のreplyButtonをクリックする必要があります。

enter image description here

コード:テキスト値を取得しようとしている方法とあまり間違ってないです

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 


    @IBAction func replyButtonTapped(_ sender: Any) { 

    self.performSegue(withIdentifier: "toReplyVC", sender: self) 

} 


    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 

    if segue.identifier == "toReplyVC" { 

     let replyVC = segue.destination as! ReplyVC 

    // code below did't work: 

    /* 

     let indexPath = self.repliesTableView.indexPathForSelectedRow 

      let cell = self.repliesTableView.cellForRow(at: indexPath!) as! ReplyCell 

      replyVC.replyQuote = cell.replyTextView.text 

    */  
     } 
} 


} 
+0

クラッシュレポートはありますか、値はゼロですか?あなたの問題についていくつか説明してください。インデックスパスがnilであるため、 – emresancaktar

+0

がクラッシュしました。 – developermike

答えて

0

あなたはReplycell.swiftは、細胞内のボタンアクション内でこの

weak var delegate: ReplycellDelegate? 

を追加し、クラスの内部クラス

protocol ReplycellDelegate: class { 
func didPressButtonFromCell(cellView: Replycell, id: int)} 

前にこれを追加するには、プロトコルを使用する必要がありますこれを追加する

delegate?.didPressButtonFromCell(cellView: self, id: 2) 

を呼び出して、uitableviewdatasourceの後にViewControllerクラスにRepplyCellDelegateを追加してから、関数didPressButtonFromCellを呼び出してXcodeで自動補完すると、セルのボタンが押されたときにその関数が呼び出されます。ビューコントローラのセルはid変数をRepplycellクラスに追加するので、作成時に各セルに渡すことができます。プロトコルのidではなくReplycellクラスのテキストを取得し、文字列を宣言して、 ViewController。

0

、あなただけ間違った関数の中にそれをやっています。 UITableViewには、機能するdidSelectRowAt:indexPathがあります。

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 

     let cell = tableView.cellForRow(at: indexPath) as! Replycell 
     replyVC.replyQuote = cell.replyTextView.text 


    } 
+0

こんにちはエリック、私はあなたが意味することを理解しています。セル全体ではなく、セル内のreplyButtonをクリックする必要があります。だから私didSelectRowAt indexPath関数を使用していない。 – developermike

関連する問題