swift - UITableView didSelectRowAtIndexPath 获取所选部分的实例

标签 swift uitableview didselectrowatindexpath

有没有办法获取选择行的部分的实例?获取section的index,selected cell的index,selected cell的实例是可以的。但是这个section的实例呢?

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 - UITableView didSelectRowAtIndexPath 获取所选部分的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35864352/

相关文章:

ios - 未调用 UITableView didSelectRow

objective-c - Segued 时如何获取我的 UITableViewController 实例?

ios6 - 如何以编程方式向下滑动 UITableView 以显示底层 UIRefreshControl

ios - AVAudioFile.write(来自 :) fails when buffer contains interleaved audio

ios - 为什么在我的每个新 xcode 项目中都会显示两个 Storyboard?

ios - didSelectRowAtIndexPath返回界限索引

ios - Swift - 选择 TableView 单元格时动画(didSelectRowAtIndexPath)

iphone - 为什么我无法从 UITableView 单元格转至详细 View ?

ios - 将数据传递给导航 Controller 中的另一个 View Controller

swift - UICollectionView 未从数组中填充数据