ios - 为每个 TableView 单元执行不同的 segue

标签 ios swift if-statement tableview

我有一个填充了多个动态单元格的 TableView。我有代码在点击一个单元格后执行 segue,但是我需要这样做以便每个单元格执行不同的 segue。有人可以帮助我吗,因为无论我按哪个单元格,我正在使用的代码都会继续执行第一个 segue!

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    NSLog("You selected cell number: \(indexPath.row)!")
    if indexPath.row == 0
    {
        self.performSegueWithIdentifier("MoveToSignIn", sender: self)
    }
    if indexPath.row == 1
    {
        self.performSegueWithIdentifier("MoveTo2", sender: self)
    }
}

最佳答案

这里发生的事情(我很确定这就是问题所在)是部分。一张表不可能有两个单元格具有相同的部分和行值。解决方案应该是这样的:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    NSLog("You selected cell number: \(indexPath.row)!")
    if indexPath.row == 0 && indexPath.section == 0
    {
        self.performSegueWithIdentifier("MoveToSignIn", sender: self)
    }
    if indexPath.row == 0 && indexPath.section == 1
    {
        self.performSegueWithIdentifier("MoveTo2", sender: self)
    }
}

Sections 只是重置你的行,所以很容易混淆,因为你有 5 行但是两个部分或类似的东西。

关于ios - 为每个 TableView 单元执行不同的 segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28433645/

相关文章:

ios - addSubview 上的 Objective-C 内存泄漏

ios - 使用来自 GIDSignIn 的访问 token 来使用 Google Drive SDK iOS

ios - 带有自定义部分的 UILocalizedIndexedCollat​​ion

ios - 尝试临时分发 ios 应用程序时出现 OSStatus 错误 100021

xcode - 我如何加载 viewController 而不是 xib 文件

swift - UITableView 慢

ios - swift 3 : Filtering IOS notifications

bash - 我怎样才能grep变量中的复杂字符串?

excel - 组合多列的 IF 语句

Java应用if/case推荐