Getting object for tapped UITableViewCell in new UIView?

删除回忆录丶 提交于 2019-12-25 06:32:40

问题


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]
    }
}


来源:https://stackoverflow.com/questions/35849931/getting-object-for-tapped-uitableviewcell-in-new-uiview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!