是否有更短的方法来使用indexPath.row?

时间:2016-01-03 22:54:45

标签: 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)

    }
}

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

1 个答案:

答案 0 :(得分:1)

你可以这样做

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