2015-06-22 61 views
0

我想獲取自定義單元格中所選行的名稱。我如何在Swift中做到這一點?如何獲得自定義單元格標籤時,segue?

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 

    var selectedCell = CustomCell() 
    selectedCell = tableView(tbl, cellForRowAtIndexPath: <#NSIndexPath#>) 

    if(segue.identifier == "next") { 
     var next = (segue.destinationViewController as! EngineViewController) 
     next.titleSub = selectedCell.cell.lblBrand.text 
    } 
} 
+0

你填充與數據源的表視圖?如果是這樣,請從您的數據源集合中獲取相應索引的值。 –

+0

你的控制器是否繼承自UITableViewController? – Carpsen90

+0

不,它是一個UIViewController @ Carpsen90 – CAN

回答

1

我相信如果你只是選擇一個單元格,單元格應該是發件人。試試這個

if let selectedCell = sender as? CustomCell { 
    if(segue.identifier == "next") { 
    var next = (segue.destinationViewController as! EngineViewController) 
    next.titleSub = selectedCell.cell.lblBrand.text 
    } 
} 
0

你可以嘗試覆蓋didSelectRowAtIndexPath來獲取你的單元格。

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
let cell = tableView.dequeueReusableCellWithIdentifier("CellIdentifier", forIndexPath: indexPath) as! CustomCell 
    cell.yourLabel.textYouWant 
let yourViewController = storyboard?.instantiateViewControllerWithIdentifier("ViewController") as! ViewController 
    navigationController?.pushViewController(yourViewController, animated: true) 

}

+0

我不能覆蓋(方法不覆蓋任何方法從它的超類),我使用segue – CAN

相關問題