ios - 有没有更短的方法来使用 indexPath.row?

标签 ios swift uitableview didselectrowatindexpath

indexPath.row 有更短的使用方法吗?我目前正在使用它:

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

    if indexPath.row == 0 {

        self.performSegueWithIdentifier("0", sender: self)
        tableView.deselectRowAtIndexPath(indexPath, animated: true)

    } else if indexPath.row == 1 {

        self.performSegueWithIdentifier("1", sender: self)
        tableView.deselectRowAtIndexPath(indexPath, animated: true)

    } else if indexPath.row == 2 {

        self.performSegueWithIdentifier("2", sender: self)
        tableView.deselectRowAtIndexPath(indexPath, animated: true)

    } else if indexPath.row == 3 {

        self.performSegueWithIdentifier("3", sender: self)
        tableView.deselectRowAtIndexPath(indexPath, animated: true)

    } else if indexPath.row == 4 {

        self.performSegueWithIdentifier("4", sender: self)
        tableView.deselectRowAtIndexPath(indexPath, animated: true)

    } else if indexPath.row == 5 {

        self.performSegueWithIdentifier("5", sender: self)
        tableView.deselectRowAtIndexPath(indexPath, animated: true)

    } else if indexPath.row == 6 {

        self.performSegueWithIdentifier("6", sender: self)
        tableView.deselectRowAtIndexPath(indexPath, animated: true)

    } else if indexPath.row == 7 {

        self.performSegueWithIdentifier("7", sender: self)
        tableView.deselectRowAtIndexPath(indexPath, animated: true)

    } else if indexPath.row == 8 {

        self.performSegueWithIdentifier("8", sender: self)
        tableView.deselectRowAtIndexPath(indexPath, animated: true)

    } else if indexPath.row == 9 {

        self.performSegueWithIdentifier("9", sender: self)
        tableView.deselectRowAtIndexPath(indexPath, animated: true)

    } else if indexPath.row == 10 {

        self.performSegueWithIdentifier("10", sender: self)
        tableView.deselectRowAtIndexPath(indexPath, animated: true)

    }
}

我实际上在我的项目中使用了 42 条 indexPath.row 语句,但我只举了上面的 11 条作为示例。

最佳答案

你可以这样做

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    self.performSegueWithIdentifier(String(indexPath.row), sender: self)
    tableView.deselectRowAtIndexPath(indexPath, animated: true)
}

关于ios - 有没有更短的方法来使用 indexPath.row?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34582574/

相关文章:

swift 错误 : Realm accessed from incorrect thread

ios - swift 3 : Is there a way to cast an object to a class and protocol at the same time?

ios - 检测 UITableView 部分标题何时捕捉到屏幕顶部

ios - 向下滚动页面一次后显示表格 View 按钮

sql - UITableView 中来自 SQLite 的 iOS NSArray 的 NSDictionaries

ios - 来自模态转场的 SWRevealViewController

ios - 如何将气球图像设置到 View 的右侧?

ios - 检索自定义单元格内的 textfield.text

ios - 最低操作系统版本不匹配

ios - 实例成员已声明时不能使用