我想呈現一個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。
是因爲indexPath更準確的原因嗎?或者這只是恰當的做法?並且還感謝您 – Ace
當您比較整數而不是字符串時,它更準確。以及它的最佳實踐,假設您隨時更改了顯示的文本,但在字符串的情況下,您必須更改比較代碼,但是在使用indexPath的情況下,您不需要使用其他的指示符 –
。其他名稱或其他想法? – jcmaxuser