2015-10-18 53 views
0

我想呈現一個viewController取決於哪個tableView單元格被點擊。針對不同的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嗎?如如果(cellLabel.text == 「選項8」){ FUNC的tableView(的tableView:UITableView的,didSelectRowAtIndexPath方法indexPath:NSIndexPath){ self.performSegueWithIdentifier( 「選項8」,發送方:自) }

}

我正在使用Swift和xcode7。

回答

0

請不要和cellLabel.text做比較,請考慮使用indexPath.row。然後爲您需要的每個視圖控制器在故事板中添加不同的細分。代碼將是這樣的:

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

是因爲indexPath更準確的原因嗎?或者這只是恰當的做法?並且還感謝您 – Ace

+0

當您比較整數而不是字符串時,它更準確。以及它的最佳實踐,假設您隨時更改了顯示的文本,但在字符串的情況下,您必須更改比較代碼,但是在使用indexPath的情況下,您不需要使用其他的指示符 –

+0

。其他名稱或其他想法? – jcmaxuser

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,根據行​​號您可以重定向到特定的控制器。

相關問題