【问题标题】:How to get the indexPath of a UITableView Row in edit mode如何在编辑模式下获取 UITableView 行的 indexPath
【发布时间】:2014-03-03 18:04:50
【问题描述】:
有没有办法获得处于编辑模式的行?
我知道我可以在这里得到它- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
但是我该如何摆脱这种方法呢?
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 不起作用,因为它返回 NULL...
当我指的是编辑模式时,这意味着该行已向左移动并在右侧显示删除...
提前致谢。
【问题讨论】:
标签:
objective-c
uitableview
ios7
editmode
【解决方案1】:
indexPathForSelectedRow 不起作用,因为该行未被选中。
定义一个属性来保存选择删除的当前行:
@property (nonatomic, strong) NSIndexPath *indexPathOfDeleteRow;
然后使用tableView:commitEditingStyle:forRowAtIndexPath更新属性:
- (void) tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
self.indexPathOfDeleteRow = indexPath;
}
【解决方案2】:
您可以使用这个UITableView 扩展:
extension UITableView {
var indexPathForEditingRow: NSIndexPath? {
return indexPathsForEditingRows.first
}
var indexPathsForEditingRows: [NSIndexPath] {
return visibleCells.flatMap { cell -> NSIndexPath? in
guard let indexPath = indexPathForCell(cell) where cell.editingStyle != .None else {
return nil
}
return indexPath
}
}
}