【问题标题】:How To use different segues for different UTableView Sections and pass data如何对不同的 UTableView 部分使用不同的 segue 并传递数据
【发布时间】:2026-01-22 20:40:01
【问题描述】:

我使用带有两个部分的UITableView,我使用一个名为sections 的数组作为部分标题标题,一个名为sectionItems 的数组包含两个字符串值数组来填充这些部分。

let sections = ["Section 1", "Section 2"]

var sectionItems = [ ["One","Two", "Three", "Four"], ["Another One"] ]

UITableView 可以很好地显示数据,但是当用户选择表格视图单元格时,我试图继续。问题是我对两个不同的部分有两个不同的 segue。

如何将每个 segue 用于适当的部分?

这就是我正在尝试的,

 override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell

    tableView.deselectRowAtIndexPath(indexPath, animated: true)


    //Optional error occurs here for section property 
    let section = self.tableView.indexPathForSelectedRow!.section

    if section == 0 {
        self.performSegueWithIdentifier("sectionOneSegue", sender: cell)
    } else if section == 1 {
        self.performSegueWithIdentifier("sectionTwoSegue", sender: cell)
    }

}

但是,在声明属性部分时,这给了我一个可选错误。

此外,当启动 segue 时,我试图将相关数据传递给细节控制器。

这里我试图确保在 sectionItems 数组中我使用数组中的第一个数组,方法是使用节号,然后是所选单元格行的确切字符串值。

    if segue.identifier == "sectionOneSegu" {

        let navController = segue.destinationViewController as! UINavigationController
        let detailController = navController.topViewController as! DetailViewController
        var row = self.tableView.indexPathForSelectedRow!.row


        detailController.upcomingType = sectionItems[1][row]            
        detailController.mode = 1

    }

我不确定这是否正确。有人可以帮助我了解为什么在选择单元格行时会发生错误以及如何修复它以使用正确的 segue 吗?以及如何传递相关数据?

【问题讨论】:

  • 获取章节号,你可以使用:indexPath.section
  • 您可以将 segues 链接到原型单元格,然后当您触摸一个单元格时,它将调用您的 prepareForSegue 方法,您可以在其中检查 segue.identifier 属性。从那里您将像现在一样提取选定的行并从表数据中获取数据。看起来所选行的索引路径为零,这就是您无法提取部分值的原因。在您当前的示例中,这无关紧要,因为您将 indexPath 作为参数。我不知道苹果何时更新要选择的单元格的状态,但有可能直到 didSelectRow 之后
  • 仅供参考,您永远不应该从 cellForRowAtIndexPath 外部调用 dequeueReusableCellWithIdentifier

标签: ios arrays swift uitableview


【解决方案1】:

首先,您不需要在“didSelectRowAtIndexPath”方法中获取单元格,因此创建单元格引用的第一行是不必要的。

您有一个索引路径,其中包含行和部分索引,使用它从 sectionItems 数组中获取数据,例如

let data = sectionItems[indexPath.section][indexPath.row]

switch indexPath.section {
case 0:
    self.performSegueWithIdentifier("sectionOneSegue", sender: data)
case 1:
    self.performSegueWithIdentifier("sectionTwoSegue", sender: data)
default:
    break
}

这应该是您在该方法中所需要的。

然后您可以覆盖 prepareForSegue 方法并检查 segue.identifier 属性以查看执行了哪个 segue 并从中提取 destinationViewController。拥有视图控制器后,您可以通过视图控制器上的属性将数据传递给它。

if segue.identifier == "sectionOneSegue" {

    guard let data = sender as? String,
    newViewController = segue.destinationViewController as? NewViewController else {
        return
    }

    newViewController.data = data
}

在上面的代码中,我确保发送者是预期的数据类型(从上面的 performSegueWithIdentifier 方法发送的那个)并且目标控制器是我想要的,然后一旦我知道一切都是正确,我正在使用要发送给它的数据设置目标控制器上的属性。

我希望这会有所帮助。

【讨论】:

  • 我还应该补充一点,您可能应该将标识符移动到某个地方的常量,这样您就不会因拼写错误等而出现任何意外错误。
  • 另外请记住,我编写“NewViewController”只是为了演示一个实现,您应该将其替换为您正在使用的实际视图控制器类的名称;)
  • 顺便说一句,我在获取行值时仍有问题,我收到以下let indexPath = tableView.indexPathForSelectedRow let selectedObject = Objects[indexPath!.row] 的可选错误
  • 我正在尝试使用部分中的行值将 NSManagedObject 从数组传递到目标视图控制器
  • 你把let indexPath = ...放在哪里?索引路径在“didSelectRowAtIndexPath”的方法参数中提供给您。
【解决方案2】:

因为你在tableView:didSelectRowAtIndexPath:方法里面,你不需要像这样寻找选择的索引路径:

let section = self.tableView.indexPathForSelectedRow!.section

所需的indexPath 以变量形式提供给您,因此您只需

if indexPath.section == 0 {
    self.performSegueWithIdentifier("sectionOneSegue", sender: cell)
} else if indexPath.section == 1 {
    self.performSegueWithIdentifier("sectionTwoSegue", sender: cell)
}

或使用一个数组,为节索引提供 segue 名称,如下所示:

private static let segueForIndex = ["sectionOneSegue", "sectionTwoSegue"]
...
self.performSegueWithIdentifier(MyClass.segueForIndex[indexPath.section], sender: cell)

【讨论】:

    最近更新 更多