我有一个UITableView,我希望能够在不处于编辑模式时对行进行重新排序,这意味着我不想看到删除图标,直到按Edit。我希望能够始终查看和使用重新排序的控件。那可能吗?在不处于编辑模式时重新排序UITableView上的控件?
我是否必须始终保持UITableView处于编辑模式并手动启用/禁用删除图标?如何在这种情况下禁用滑动删除?
我有一个UITableView,我希望能够在不处于编辑模式时对行进行重新排序,这意味着我不想看到删除图标,直到按Edit。我希望能够始终查看和使用重新排序的控件。那可能吗?在不处于编辑模式时重新排序UITableView上的控件?
我是否必须始终保持UITableView处于编辑模式并手动启用/禁用删除图标?如何在这种情况下禁用滑动删除?
据我所知,你不能只使用编辑标志。但是,获得类似效果并不困难。
有自己的布尔值,即deleting
。将表格单元格设置为cell.showsReorderControl = YES
。 然后执行移动单元格所需的所有方法。我认为你正在寻找的代码是。如果deleting == YES
返回UITableViewCellEditingStyleDelete
,否则返回UITableViewCellEditingStyleNone
。 最后,总是设置tableView.editing = YES
。如果在更改deleting
布尔之前和之后运行[tableView beginUpdates]
和[tableView endUpdates]
一切都应该正常工作。让我知道如果这没有意义,我会再试一次。
没有删除和没有缩进的重新排序。
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleNone;
}
- (BOOL)tableView:(UITableView *)tableview shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
- (BOOL)tableView:(UITableView *)tableview canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
参考:张贴由斯特凡·冯·Chossy here
还需要实现'moveRowAt:to:',否则不会显示重新排序的控件。 – Matt 2016-12-15 23:17:25
解决方案我找到了解决办法。 UITableViewCell
拒绝绘制重新排序控件,除非它处于编辑模式。幸运的是,UITableViewCell
和UITableView
分别进行了轨道编辑,关键的是,UITableView
实际上处理重新排序而不管其自己的编辑模式。我们只需要欺骗单元格绘制重新排序控件,我们就是免费的。
子类UITableViewCell
这样的:
class ReorderTableViewCell: UITableViewCell {
override var showsReorderControl: Bool {
get {
return true // short-circuit to on
}
set { }
}
override func setEditing(editing: Bool, animated: Bool) {
if editing == false {
return // ignore any attempts to turn it off
}
super.setEditing(editing, animated: animated)
}
}
现在只需对要启用重新排序细胞集editing = true
。您可以使用-tableView:canMoveRowAtIndexPath:
作为条件。
在您的表视图数据源:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
// Configure the cell...
:
cell.editing = self.tableView(tableView, canMoveRowAtIndexPath: indexPath) // conditionally enable reordering
return cell
}
唯一的缺点是,这是与表视图allowsMultipleSelectionDuringEditing
选项不兼容;编辑控件不正确地始终显示。解决方法是仅在实际表格视图编辑期间启用多项选择。
在表格视图控制器:
override func setEditing(editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)
self.tableView.allowsMultipleSelectionDuringEditing = editing // enable only during actual editing to avoid cosmetic problem
}
此外,在-viewDidLoad
或故事板,的allowsMultipleSelectionDuringEditing
的初始值设置为false。
为每个单元格设置UITableViewCellEditingStyle标志并实现canMoveRowAtIndexPath和canEditRowAtIndexPath UITableViewDataSource委托方法。不确定您是否必须永久处于编辑模式才能执行此操作。通过这种方式,将停止滑动删除 – TheBlack 2011-05-23 13:58:33