2016-11-22 1 views
0

ViewController에는 TableView가 있습니다. TableViewCell에는 콘텐츠가 포함 된 textView가 있습니다. 이제 cell에서 replyButton을 클릭 한 후 segue에서 secondViewController까지를 클릭하십시오. 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

은 indexPath가 nil이므로 오류가 발생했습니다. – developermike

답변

0

당신은 Replycell.swift 셀에 버튼 액션 내에서이

weak var delegate: ReplycellDelegate? 

를 추가 클래스의 내부 클래스

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

전에이 추가로, 프로토콜을 사용할 필요가 이 항목을 추가하십시오.

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

이제 ViewController 클래스에 ReuitlyviewDatasource를 추가 한 후 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

관련 문제