2017-12-28 22 views
0

테이블 뷰의 단추를 누르면 다른보기 컨트롤러와 연결되어 데이터를 전달해야하지만 작동하지 않는 대리자 메서드가 있습니다.테이블 뷰 셀에서 분할 방법을 위임하는 방법

func goToVC(uid: String) { //delegate method 
    performSegue(withIdentifier: "showVC", sender: self) //Do I need this 
} 

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    performSegue(withIdentifier: "showVC", sender: self) 
    self.tableView.deselectRow(at: indexPath, animated: true) 
} 

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if segue.identifier == "showVC" { 
     if let indexPath = tableView.indexPathForSelectedRow { 
      let guestVC = segue.destination as! GuestViewController 
      guestVC.ref = userArray[indexPath.row].ref 
     } 
    } 

답변

0
class MainViewController: UIViewController { 

    // set the cell's delegate in the data source 
    // pass the object to the cell from the data source 
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     cell.mainViewControllerDelegate = self 
     cell.object = someArray[indexPath.row] 

    } 

    // this is the method that gets called by the cell through the delegate 
    func pushToViewController(object: YourDataObject) { 

     let destination = SomeViewController() 
     destination.object = object 
     navigationController?.pushViewController(destination, animated: true) 

    } 

} 

class TheTableViewCell: UITableViewCell { 

    // create a delegate and a data object 
    var mainViewControllerDelegate: MainViewController? 
    var object: YourDataObject? 

    // this is the method that gets called when the button in the cell is tapped 
    @objc func buttonAction() { 

     mainViewControllerDelegate?.pushToViewController(object: object) 

    } 

} 

나는 매우 초보자 인터페이스 빌더를 사용하지 않는 것이 좋습니다. 빨리 사용하면할수록 더 빨리 이해할 수 있습니다. 인터페이스 빌더는 초보자를위한 어리 석다.

+0

감사합니다. 새보기 컨트롤러로 리디렉션 될 때 검은 화면이 표시됩니다 (오류는 표시되지 않음). 가장 좋은 해결책은 무엇입니까? –

+0

대의원이 제대로 연결되어있는 것처럼 보입니다. 먼저 navigationController? .pushViewController (destination, animated : true)를 제거하고 단순히'print ("great success")'콘솔에 출력하여 확인하십시오. 위임자가 실제로 작동하는 경우 View Builder를 Interface Builder에서 일반적으로 수행하는 방법으로 푸시합니다. 내가 제공 한 예는 프로그래밍 방식의 앱이지만 개념은 변경되지 않았습니다. – slickdaddy

+0

흠 ... 대의원은 "큰 성공"을 인쇄하면서 일하는 것 같습니다. 대리자 내의 다른보기 컨트롤러로 밀어 넣는 다른 방법은 없나요? –

0

여기에 위임 방법이 필요하지 않습니다. 자식 뷰 컨트롤러에서 값을 전달해야하는 경우 대리자 메서드를 사용할 수 있습니다.

당신이하고있는 일은 정확합니다. 스토리 보드에서 세그먼트 식별자를 올바르게 설정했는지 확인하십시오.

테이블 IBOutlet을 기본값으로 설정하지 마십시오 tableViewtoDoTable과 같은 테이블의 이름을 적절하게 설정하면 쉽게 디버깅 할 수 있습니다.