UITableView didSelectRowAtIndexPath获取所选部分的实例

时间:2016-03-08 10:05:38

标签: swift uitableview didselectrowatindexpath

有没有办法获取选择行的部分的实例?可以获取节的索引,所选单元格的索引,所选单元格的实例..但是这部分的实例?

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let indexPath = tableView.indexPathForSelectedRow // index path of selected cell

    let headerCellIndex = indexPath!.section // index of selected section
    let headerCellName = ????? // instance of selected section

    let cellIndex = indexPath!.row // index of selected cell
    let cellName = tableView.cellForRowAtIndexPath(indexPath!) //  instance of selected cell
}

谢谢。

2 个答案:

答案 0 :(得分:10)

这对我来说总是很好。我总是打开它,也可以选择我分配给它的班级,以确保我拥有正确类型的细胞。在这个例子中我有MyTableViewCell,但它当然可以是任何东西。

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if let cell = tableView.cellForRowAtIndexPath(indexPath) as? MyTableViewCell {

    }
}

答案 1 :(得分:0)

或Swift 3.0,使用

  

覆盖func tableView(_ tableView:UITableView,didSelectRowAt   indexPath:IndexPath){

     

//你的代码......

     

}

相关问题