Ios 如何在Tableview中获取指定行的IndexPath?
我有一个10行的桌面视图。因为我想得到我的TableView的第三个单元格。所以我必须得到第3行的索引XPath。我该怎么做? 我试过: 但我犯了个错误。我怎么能得到这个 编辑: 我有一张带有复选标记的桌子。在用我的代码检查所选行之前,我想取消选中最后一行:Ios 如何在Tableview中获取指定行的IndexPath?,ios,swift,uitableview,tableviewcell,Ios,Swift,Uitableview,Tableviewcell,我有一个10行的桌面视图。因为我想得到我的TableView的第三个单元格。所以我必须得到第3行的索引XPath。我该怎么做? 我试过: 但我犯了个错误。我怎么能得到这个 编辑: 我有一张带有复选标记的桌子。在用我的代码检查所选行之前,我想取消选中最后一行: func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { var cell = self.diceFac
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var cell = self.diceFaceTable.cellForRowAtIndexPath(indexPath) as! DiceFaceTableViewCell
if selectedDiceFace != indexPath.row {
var indexPathOfLastSelectedRow = NSIndexPath(forRow: selectedDiceFace, inSection: 0)
println("indextPath: \(indexPath)")
println("indextPathOfLastSelectedRow: \(indexPathOfLastSelectedRow)")
println("selectedDiceFace: \(selectedDiceFace)")
println("selectedRow: \(indexPath.row)")
var lastSelectedCell = self.diceFaceTable.cellForRowAtIndexPath(indexPathOfLastSelectedRow) as! DiceFaceTableViewCell
lastSelectedCell.selectedImg.image = UIImage(named: "uncheck")
selectedDiceFace = indexPath.row
cell.selectedImg.image = UIImage(named: "check")
}
cell.setSelected(false, animated: true)
}
我可以得到最后一个选定的单元格,取消选中该单元格,然后选中选择行。除了滚动表格,然后检查新行之外,一切正常。我将获得崩溃,错误约为“致命错误:在第行展开可选值时意外发现零”:
我不知道我的代码中有什么问题会导致我滚动表格,然后选择行。节也从0开始编号,因此您需要将
instation
指定为0:
var indexPathOfLastSelectedRow = NSIndexPath(forRow: 2, inSection: 0)
如果你只有一个部分,这是当然的。如果只在一个小案例中有效,那么答案是毫无用处的:“如果你只有一个部分,这是当然的。”
var lastSelectedCell = self.diceFaceTable.cellForRowAtIndexPath(indexPathOfLastSelectedRow) as! DiceFaceTableViewCell
var indexPathOfLastSelectedRow = NSIndexPath(forRow: 2, inSection: 0)