Ios 根据单击的tableView单元格定向到不同的ViewController

Ios 根据单击的tableView单元格定向到不同的ViewController,ios,swift,uitableview,xcode7,Ios,Swift,Uitableview,Xcode7,我试图根据单击的tableView单元格显示一个viewController 当前,如果它们都被单击,它们将重定向到同一个viewController,我将如何更改它 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = self.tableView.dequeueReusableCellWithIden

我试图根据单击的tableView单元格显示一个viewController

当前,如果它们都被单击,它们将重定向到同一个viewController,我将如何更改它

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TableViewCell
    cell.cellLabel.text = self.objects.objectAtIndex(indexPath.row) as! String

    return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    self.performSegueWithIdentifier("other", sender: self)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if(segue.identifier == "other"){

        var upcoming: otherViewController = segue.destinationViewController as! otherViewController

        let indexPath = self.tableView.indexPathForSelectedRow!

        let titleString = self.objects.objectAtIndex(indexPath.row) as! String

        upcoming.titleString = titleString

        self.tableView.deselectRowAtIndexPath(indexPath, animated: true)


    }
}
我可以在cellLabel.text上做一个if-else吗?如if(celllable.text==“option8”){ func tableView(tableView:UITableView,didSelectRowAtIndexPath:nsindepath){ self.performsguewithidentifier(“选项8”,发件人:self) }

}


我使用的是Swift和xcode7 CyLabel.Text < /代码>做比较,考虑使用<代码>然后在故事板中为所需的每个视图控制器添加不同的分段。代码如下所示:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.row == 0 || indexPath.row == 2{
   self.performSegueWithIdentifier("other", sender: self)
}
else{
  self.performSegueWithIdentifier("other", sender: self)
}

}

<>不要,也永远不要与<代码> CyLabel.Text < /代码>进行比较,考虑使用<代码>索引xPrave.Reals<代码>。然后在故事板中为所需的每个视图控制器添加不同的分段。代码如下所示:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.row == 0 || indexPath.row == 2{
   self.performSegueWithIdentifier("other", sender: self)
}
else{
  self.performSegueWithIdentifier("other", sender: self)
}

}
是的,你可以

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let cell = tableView.cellForRowAtIndexPath(indexPath)
    if cell.cellLabel.text == "option8" {
        self.performSegueWithIdentifier("other8", sender: self)
    }
}
是的,你可以

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let cell = tableView.cellForRowAtIndexPath(indexPath)
    if cell.cellLabel.text == "option8" {
        self.performSegueWithIdentifier("other8", sender: self)
    }
}

您可以根据行号检查didSelectrow中的indexpath.row,您可以重定向到特定控制器。

您可以根据行号检查didSelectrow中的indexpath.row,您可以重定向到特定控制器。

原因是因为indexpath更准确吗?或者这只是一种恰当的做法?也谢谢你,因为比较整数而不是字符串更准确。以及它的最佳实践,假设您随时更改显示的文本,对于字符串,您必须更改比较代码,但是对于使用indexPath,您不知道什么是其他。其他名称或其他想法?是因为indexPath更准确吗?或者这只是一种恰当的做法?也谢谢你,因为比较整数而不是字符串更准确。以及它的最佳实践,假设您随时更改显示的文本,对于字符串,您必须更改比较代码,但是对于使用indexPath,您不知道什么是其他。其他名字还是其他想法?