在新的UIView中获取被窃听的UITableViewCell的对象? [英] Getting object for tapped UITableViewCell in new UIView?

查看:67
本文介绍了在新的UIView中获取被窃听的UITableViewCell的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从UITableViewCell的单元格到新视图并传递参数数据的正确方法是什么?

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

我从UITableViewCell的原型单元中指定了一个序列,将其链接到新的UIVIew并将其设置为show (aka push).将显示新的UIView,但是我不确定如何使UIView知道所选择的节/行或告诉它与该选择关联的对象.

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

我发现发生的是在prepareForSegue()函数之后调用了tableView()函数,导致在调用viewWillAppear()时文件属性为nil.

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.

推荐答案

再进行一些搜索就发现了其他

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

这篇关于在新的UIView中获取被窃听的UITableViewCell的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆