根据单击的tableView单元格指向不同的ViewController

时间:2015-10-18 12:55:41

标签: ios swift uitableview xcode7

我正在尝试根据单击的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(cellLabel.text ==" option8"){  func tableView(tableView:UITableView,didSelectRowAtIndexPath indexPath:NSIndexPath){         self.performSegueWithIdentifier(" option8",sender:self)     }

}

我正在使用Swift和xcode7。

3 个答案:

答案 0 :(得分:0)

不要与cellLabel.text进行比较,考虑使用indexPath.row。然后在故事板中为您需要的每个视图控制器添加不同的segue。代码将是这样的:

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)
}

}

答案 1 :(得分:0)

是的,你可以。

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

答案 2 :(得分:0)

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

相关问题