怎么知道,哪个单元格打开了UiTableViewController?

时间:2015-06-06 16:16:06

标签: ios uitableview swift

我有两个UITableViewControllerSettingsViewControllerDetailsViewController。我想要一个Details控制器用于不同的Settings,即"语言","货币","可见性"等

所以,我需要在DetailsViewController知道SettingsViewController的哪个单元格打开它。

我在Swift方面还不是很好,所以我在Interface Builder中为SettingsViewController构建了单元格,我的代码中没有cellForRowAtIndexPath方法。我需要实现这种方法吗?或者,也许,我可以通过索引(或标签名称)获取特定单元格?但为此,我需要将整个SettingsViewController连接到DetailsViewController,我也不知道该怎么做。

事实上我在DetailsViewController

中需要这样的东西
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    # if it was called by "Language" cell
        return 2
    # if it was called by "Currency" cell
        return 5
    # else
        return 7
}

感谢您的帮助!如果您需要这些控制器的代码,我会添加它,但事实上它没有什么特别之处,只有标准return s的标准方法。

2 个答案:

答案 0 :(得分:0)

你必须实现prepareForSegue:sender:方法,因为@dasdom如下所示:

// SettingsViewController.swift
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "Storyboard Segue Identifier" {
        // then you get index with `indexPathForSelectedRow`
        if let indexPath = self.tableView.indexPathForSelectedRow() {
            let controller = segue.destinationViewController as DetailsViewController
            // configure the controller
            // controller.xxx = xxx
        }
    }
}

答案 1 :(得分:0)

您可以使用tableView的didSelectRowAtIndexPath和prepareForSegue:

let data = ["Language", "Currency", "Visibility"]
var dvcRowCount:Int?

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    switch indexPath.row {
    case 0: dvcRowCount = 2
    case 1: dvcRowCount = 3
    case 2: dvcRowCount = 4
    default: dvcRowCount = 1
    }
    performSegueWithIdentifier("showDetail", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let dvc = segue.destinationViewController as DetailTableViewController
    dvc.rowCount = dvcRowCount
}

在detailTableViewController中:

    var rowCount:Int?
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    //return 0 if the rowCount is not set
    return rowCount ?? 0
}

我上传了演示代码:https://github.com/vidaaudrey/SharingDetailTableView