有没有办法获取选择行的部分的实例?获取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/