Swift UITableView didSelectRowAtIndexPath获取所选节的实例

Swift UITableView didSelectRowAtIndexPath获取所选节的实例,swift,uitableview,didselectrowatindexpath,Swift,Uitableview,Didselectrowatindexpath,是否有方法获取选中行的节的实例?可以获取节的索引、所选单元格的索引、所选单元格的实例..但是该节的实例 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { let indexPath = tableView.indexPathForSelectedRow // index path of selected cell let headerCellIndex

是否有方法获取选中行的节的实例?可以获取节的索引、所选单元格的索引、所选单元格的实例..但是该节的实例

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
}

谢谢。

这对我一直都很有效。我总是有选择地将它展开到我分配给它的类中,以确保我有正确类型的单元格。在这个例子中,我有MyTableViewCell,但它当然可以是任何东西

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
   if let cell = tableView.cellForRow(at: indexPath) as? MyTableViewCell {
         print(cell.label.text!)
    }
}
或Swift 3.0,使用

重写func tableView(tableView:UITableView,didSelectRowAt indepath:indepath){

//你的代码

}


通过函数DidSelectRows,您可以使用如下开关(例如3个部分,可变行)


加载tableview时如何插入节的名称?
String(content[section].sectionName)
这也适用于
didSelectRowAtIndexPath
-方法。但是,是否有一种方法可以获取节的名称,以某种方式类似于获取所选单元格的名称?内容是数组/对象吗?为什么不在didSelectRow中尝试
var secName=content[indepath.section].sectionName没有节实例,您不应该从单元格中获取标签文本-始终从数据模型中获取数据,只获取单元格实例,这样您就可以动态更新来自web服务的节,如何获取indepath。
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
}