我正在尝试从表格中的选定行中提取所有数据。我使用以下代码来打印indexPaths。但是,我需要做什么来打印所选行中的文本?
let indexPaths:NSArray = tableView.indexPathsForSelectedRows()!
`for var i = 0; i < indexPaths.count; ++i {
var thisPath:NSIndexPath = indexPaths.objectAtIndex(i) as NSIndexPath
println("row = \(thisPath.row) and section = \(thisPath.section)")
}
打印以控制所选行
row = 1 and section = 0
row = 3 and section = 0
row = 6 and section = 0
答案 0 :(得分:3)
您可以使用cellForRowAtIndexPath
的{{1}}方法按索引路径获取单元格:
UITableView
请注意if let indexPaths = tableView.indexPathsForSelectedRows() {
for var i = 0; i < indexPaths.count; ++i {
var thisPath = indexPaths[i] as NSIndexPath
var cell = tableView.cellForRowAtIndexPath(thisPath)
if let cell = cell {
// Do something with the cell
// If it's a custom cell, downcast to the proper type
}
}
}
返回一个可选项(当没有选择行时为nil),因此最好使用可选绑定来保护它,以防止运行时异常。