首页 文章

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

提问于
浏览
1

我试图提出一个viewController取决于单击哪个tableView单元格 .

目前,如果它们全部被点击,它们会重定向到同一个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

    不要也永远不要与_904666做比较,考虑使用 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)
    }
    
    }
    
  • 0

    是的你可以 .

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

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

相关问题