Getting object for tapped UITableViewCell in new U

2019-09-11 12:44发布

问题:

What is the right way to segue from a cell in a UITableViewCell to a new view, passing parameter data?

I have specified a segue from my prototype cell in my UITableViewCell, that links it to a new UIVIew and has it set to show (aka push). The new UIView displays, but I am not sure how to make tell the UIView the section/row that was selected or tell it about the object associated with that selection.

An abridged version of my code is shown below:

class MyTableViewController : UITableViewController {

    // other code omitted

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let files = filesByDate[filesByDateKeys![indexPath.section]]
        self.selectedFile = files![indexPath.row]
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        if (segue.identifier == "SegueToFileDetails") {
            let destinationViewController = segue.destinationViewController as! FileDetailsViewController
            destinationViewController.file = self.selectedFile
        }
    }
}

class FileDetailsViewController : UIViewController {
    var file: NSURL

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        print("File: ", file)
    }
}

What I find happening is the tableView() function is called after the prepareForSegue() functon, causing the file attribute to be nil when viewWillAppear() is called.

回答1:

A little more search turned up this other post, which indicated the right approach. The tableView() function in my question is not needed. Instead (in Swift 2):

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if (segue.identifier == "SegueToFileDetails") {
        let destinationViewController = segue.destinationViewController as! FileDetailsViewController

        let path = self.tableView.indexPathForSelectedRow
        let files = filesByDate[filesByDateKeys![path!.section]]

        destinationViewController.file = files![path!.row]
    }
}

And if you want to simply do behaviour based on the specific destination class, based on another suggestion:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if let destinationViewController = segue.destinationViewController as? FileDetailsViewController {

        let path = self.tableView.indexPathForSelectedRow
        let files = filesByDate[filesByDateKeys![path!.section]]

        destinationViewController.file = files![path!.row]
    }
}