如何在表视图中实现简单的向下钻取层次结构?

时间:2022-10-30 09:14:05

What method must I use to achieve a simple drill down data hierarchy in a UITableView, without having to add an unnecessary amount of table views in my story board? I want each cell in my master table view to transition to its own table view with its own cells when selected, and to do this without having to have multiple table views in my scene. Using SWIFT. FOR EXAMPLE:

我必须使用什么方法在UITableView中实现简单的向下钻取数据层次结构,而不必在我的故事板中添加不必要数量的表视图?我希望我的主表视图中的每个单元格在选择时都有自己的单元格转换到自己的表格视图,并且无需在我的场景中拥有多个表格视图。使用SWIFT。例如:

如何在表视图中实现简单的向下钻取层次结构?

1 个解决方案

#1


If the controllers you want to transition to are as you've drawn them (i.e., you always want to segue to either A or B), you don't have to duplicate A or B; you can just have one instance each of them in the storyboard and segue to it. If the controllers you want to transition to are all the same class but need some setup before transitioning, you can implement prepareForSegue(_:sender:) something like this:

如果要转换的控制器与您绘制的控制器一样(即,您总是想要转换为A或B),则不必复制A或B;你可以在故事板中分别拥有一个实例,然后转向它。如果要转换的控制器都是相同的类,但在转换之前需要进行一些设置,则可以实现prepareForSegue(_:sender :),如下所示:

func prepareForSegue(_ segue: UIStoryboardSegue, sender sender: AnyObject) { 
    let selectedIndex = self.tableView.indexPathForCell(sender as UITableViewCell)
    // set up segue.destinationViewController
}  

If there are more classes of controllers you might want to transition to, then you can do something a little more complicated and handle selections programmatically. Override tableView:didSelectRowAtIndexPath:

如果您可能想要转换到更多类别的控制器,那么您可以执行一些更复杂的操作并以编程方式处理选择。覆盖tableView:didSelectRowAtIndexPath:

func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    var vc: ViewController? = nil
    switch indexPath.row { 
        case 0: vc = ViewControllerA()
        case 1: vc = ViewControllerB()
        case 2: vc = ViewControllerC()
        ...
    } 
    navigationController?.pushViewController(vc, animated: true)
}

#1


If the controllers you want to transition to are as you've drawn them (i.e., you always want to segue to either A or B), you don't have to duplicate A or B; you can just have one instance each of them in the storyboard and segue to it. If the controllers you want to transition to are all the same class but need some setup before transitioning, you can implement prepareForSegue(_:sender:) something like this:

如果要转换的控制器与您绘制的控制器一样(即,您总是想要转换为A或B),则不必复制A或B;你可以在故事板中分别拥有一个实例,然后转向它。如果要转换的控制器都是相同的类,但在转换之前需要进行一些设置,则可以实现prepareForSegue(_:sender :),如下所示:

func prepareForSegue(_ segue: UIStoryboardSegue, sender sender: AnyObject) { 
    let selectedIndex = self.tableView.indexPathForCell(sender as UITableViewCell)
    // set up segue.destinationViewController
}  

If there are more classes of controllers you might want to transition to, then you can do something a little more complicated and handle selections programmatically. Override tableView:didSelectRowAtIndexPath:

如果您可能想要转换到更多类别的控制器,那么您可以执行一些更复杂的操作并以编程方式处理选择。覆盖tableView:didSelectRowAtIndexPath:

func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    var vc: ViewController? = nil
    switch indexPath.row { 
        case 0: vc = ViewControllerA()
        case 1: vc = ViewControllerB()
        case 2: vc = ViewControllerC()
        ...
    } 
    navigationController?.pushViewController(vc, animated: true)
}