UITableViewNavigation问题,在segue上传递数据

时间:2019-03-31 21:14:46

标签: ios swift

我无法将数据从UITableView传递到UIViewController。尝试了几种方法但是失败了。收到此错误

  

线程1:EXC_BAD_INSTRUCTION(代码= EXC_I386_INVO)

当我运行项目时。

请参阅下面我尝试过的代码。

//Code on the UITableViewController

class ShonaHymnsUITableViewController: UITableViewController {
    var hymns : [Hymns] = Hymns.fetchHymns()

    //MARK: - UItableViewDataSource
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return hymns.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "hymnCell", for: indexPath) as! HymnsTableViewCell

        let hymn = hymns[indexPath.row]
        cell.hymn = hymn
        return cell
    }

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

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // TODO: Prepare for Hymn Detal segue
        if segue.identifier == "ShowShonaHymnListing" {
            let shonaHymnListingVC = segue.destination as! ShonaHymnsUITableViewController
            shonaHymnListingVC.hymns = hymns

        } else if segue.identifier == "ShowHymnDetail" {

            let indexPath = tableView.indexPathForSelectedRow
            let selectedHymn = hymns[(indexPath?.row)!]
            let hymnDetailVC = segue.destination as! ShonaHymnsViewController
               hymnDetailVC.hymn = selectedHymn
            }
    }
}

//Code on the ViewController that I want to receive the data
class ShonaHymnsViewController: UIViewController {


    @IBOutlet weak var hymnNumber: UILabel!
    @IBOutlet weak var hymnTitle: UILabel!
    @IBOutlet weak var hymnDetail: UITextView!

    var hymn: Hymns!

    override func viewDidLoad() {
        super.viewDidLoad()

        hymnNumber.text = hymn.hymnNumber
        hymnTitle.text = hymn.hymnTitle
        hymnDetail.text = hymn.hymnDetail

    }

}

这是一本赞美诗书应用程序,我希望完整的赞美诗详细信息显示在收件人视图控制器上。 See image for the setup of the app

1 个答案:

答案 0 :(得分:0)

相反的顺序

self.performSegue(withIdentifier: "ShowHymnDetail", sender: indexPath)
tableView.deselectRow(at: indexPath, animated: true) 

为此

hymns[(indexPath?.row)!]

具有有效值