Ios 如何为不同的UTableView节使用不同的分段并传递数据

Ios 如何为不同的UTableView节使用不同的分段并传递数据,ios,arrays,swift,uitableview,Ios,Arrays,Swift,Uitableview,我将UITableView用于两个部分,我将一个名为sections的数组用于部分标题,以及一个名为sectionItems的数组,该数组包含两个字符串值数组以填充部分 let sections = ["Section 1", "Section 2"] var sectionItems = [ ["One","Two", "Three", "Four"], ["Another One"] ] UITableView可以很好地显示数据,但是当用户选择一个表视图单元格时,我会尝试进行分割。问题是

我将
UITableView
用于两个部分,我将一个名为
sections
的数组用于部分标题,以及一个名为
sectionItems
的数组,该数组包含两个字符串值数组以填充部分

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

var sectionItems = [ ["One","Two", "Three", "Four"], ["Another One"] ]
UITableView可以很好地显示数据,但是当用户选择一个表视图单元格时,我会尝试进行分割。问题是我对这两个不同的部分有两个不同的分段

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

var sectionItems = [ ["One","Two", "Three", "Four"], ["Another One"] ]
如何将每个序列用于相应的部分

这就是我想做的

 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

    }

我不确定这是否正确。有人能帮我解释一下为什么在选择单元格行时会出现错误,以及如何修复它以使用正确的序列吗?我如何传递相关数据

因为您在
tableView:didSelectRowAtIndexPath:
方法中,所以不需要像下面这样查找选择的索引路径:

let section = self.tableView.indexPathForSelectedRow!.section
private static let segueForIndex = ["sectionOneSegue", "sectionTwoSegue"]
...
self.performSegueWithIdentifier(MyClass.segueForIndex[indexPath.section], sender: cell)
所需的
indepath
在变量中提供给您,因此您只需

if indexPath.section == 0 {
    self.performSegueWithIdentifier("sectionOneSegue", sender: cell)
} else if indexPath.section == 1 {
    self.performSegueWithIdentifier("sectionTwoSegue", sender: cell)
}
或者使用一个数组为节索引提供segue名称,如下所示:

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

由于您在
tableView:didSelectRowAtIndexPath:
方法中,所以不需要像下面这样查找选择的索引路径:

let section = self.tableView.indexPathForSelectedRow!.section
private static let segueForIndex = ["sectionOneSegue", "sectionTwoSegue"]
...
self.performSegueWithIdentifier(MyClass.segueForIndex[indexPath.section], sender: cell)
所需的
indepath
在变量中提供给您,因此您只需

if indexPath.section == 0 {
    self.performSegueWithIdentifier("sectionOneSegue", sender: cell)
} else if indexPath.section == 1 {
    self.performSegueWithIdentifier("sectionTwoSegue", sender: cell)
}
或者使用一个数组为节索引提供segue名称,如下所示:

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

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

您有一个索引路径,其中包含一行和一节索引,使用它可以从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
}
在上面的代码中,我确保发送方是预期的数据类型(从上面的performsguewithidentifier方法发送的数据),并且目标控制器是我想要的,然后在我知道一切都正确后,我就在目标控制器上用我想要发送给它的数据设置属性


我希望这会有所帮助。

首先,您不需要在“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
}
在上面的代码中,我确保发送方是预期的数据类型(从上面的performsguewithidentifier方法发送的数据),并且目标控制器是我想要的,然后在我知道一切都正确后,我就在目标控制器上用我想要发送给它的数据设置属性


我希望这会有所帮助。

要获取节号,您可以使用:
indepath.section
。您可以将segues链接到原型单元格,然后当您触摸单元格时,它将调用prepareForSegue方法,您可以在其中检查segue.identifier属性。从那里,您可以像现在一样提取所选行,并从表数据中获取数据。看起来所选行的索引路径是nil,这就是您无法提取节值的原因。在您当前的示例中,这是不相关的,因为您有indexath作为参数。我不知道苹果什么时候会更新要选择的单元格的状态,但有可能直到didSelectRowFYI之后,你才应该从CellForRowAtIndexPath外部调用dequeueReusableCellWithIdentifier来获取节号,你可以使用:
indexPath.section
。你可以将节链接到原型单元格,然后,当您触摸一个单元格时,它将调用prepareForSegue方法,您可以在其中检查segue.identifier属性。从那里,您可以像现在一样提取所选行,并从表数据中获取数据。看起来所选行的索引路径是nil,这就是您无法提取节值的原因。在您当前的示例中,这是不相关的,因为您有indexath作为参数。我不知道苹果什么时候会更新要选择的单元格的状态,但可能要等到didSelectRowFYI之后,您永远不应该从CellForRowatineXpath外部调用dequeueReusableCellWithIdentifier。我还应该补充一点,您可能应该将标识符移动到某个常量,这样您就不会因为拼写错误等而出现任何意外错误。还请记住,我编写“NewViewController”只是为了演示一个实现,应将其替换为正在使用的实际视图控制器类的名称;)顺便说一句,我在获取行值方面仍然存在问题,我得到了以下可选错误:let indexath=tableView.indexPathForSelectedRow let selectedObject=Objec