2016-03-08 54 views
1

的情况下有没有办法让在其中选择一排段的实例?有可能获得该部分的索引,所选单元格的索引,所选单元格的实例......但本节的实例?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 
} 

谢谢。

+0

如何您插入节时的tableview负载的名字吗? – Lee

+0

'字符串(内容[段] .sectionName)' 这也将在'didSelectRowAtIndexPath'-method..But工作是不是有办法让节的名称在某种程度上类似于你怎么弄的名称选中的单元格? – manu

+0

是数组/对象的内容吗? – Lee

回答

5

这总是工作很适合我。我总是将它打开以及选择分配给它的班级,以确保我拥有正确类型的单元格。在这个例子中,我有MyTableViewCell,但它当然可以是任何东西。

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

    } 
} 
0

或雨燕3.0,使用

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

//你的代码...

}

0

随着功能DidSelectRows哟你可以使用这样的开关(例如, 3段,变行)

switch indexPath.section { 
case 0: 
    switch indexPath.row { 
     case 0: 
     Action to do with first cell (ex. performSegue(withIdentifier: "", sender: nil) 
     case 1: 
     Action to do with second cell 
     default: 
     break 
     } 
case 1: 
    Things you want to do in the second section 
case 2: 
    Things you want to do in the third section 
default: 
break 
} 
+0

部分动态地来自web服务,我如何获得indexpath。 – saravanar

相关问题