segue时如何获得自定义单元格标签?

时间:2015-06-22 13:12:57

标签: ios swift uitableview

我正在尝试获取自定义单元格中所选行的名称。我怎么能在Swift中做到这一点?

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

2 个答案:

答案 0 :(得分:1)

我相信如果你只是选择一个单元格,那么单元格应该是发送者。试试这个

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

答案 1 :(得分: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)

}