2011-05-23 46 views
10

我有一个UITableView,我希望能够在不处于编辑模式时对行进行重新排序,这意味着我不想看到删除图标,直到按Edit。我希望能够始终查看和使用重新排序的控件。那可能吗?在不处于编辑模式时重新排序UITableView上的控件?

我是否必须始终保持UITableView处于编辑模式并手动启用/禁用删除图标?如何在这种情况下禁用滑动删除?

+0

为每个单元格设置UITableViewCellEditingStyle标志并实现canMoveRowAtIndexPath和canEditRowAtIndexPath UITableViewDataSource委托方法。不确定您是否必须永久处于编辑模式才能执行此操作。通过这种方式,将停止滑动删除 – TheBlack 2011-05-23 13:58:33

回答

7

据我所知,你不能只使用编辑标志。但是,获得类似效果并不困难。

有自己的布尔值,即deleting。将表格单元格设置为cell.showsReorderControl = YES。 然后执行移动单元格所需的所有方法。我认为你正在寻找的代码是。如果deleting == YES返回UITableViewCellEditingStyleDelete,否则返回UITableViewCellEditingStyleNone。 最后,总是设置tableView.editing = YES。如果在更改deleting布尔之前和之后运行[tableView beginUpdates][tableView endUpdates]一切都应该正常工作。让我知道如果这没有意义,我会再试一次。

+5

。 – mackross 2011-05-25 16:44:43

+0

我的确喜欢这样。很好地工作。不过,我没有使用滑动删除。 – Shades 2011-05-26 08:10:16

+0

我都试过,但我仍需要实现: ** tableview.editing = YES; ** ,它显示的红色按钮,删除.. 我想只是重新排序细胞无任何编辑功能。 ..你可以提供一些示例代码:[email protected] – 2012-05-14 11:42:03

7

没有删除和没有缩进的重新排序。

- (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

+0

还需要实现'moveRowAt:to:',否则不会显示重新排序的控件。 – Matt 2016-12-15 23:17:25

9

解决方案我找到了解决办法。 UITableViewCell拒绝绘制重新排序控件,除非它处于编辑模式。幸运的是,UITableViewCellUITableView分别进行了轨道编辑,关键的是,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。

+0

此解决方案适用于我,谢谢! – sebastien 2016-08-28 13:45:15

+0

天才。已经通过10+ SO解决方案,这是唯一的工作。将来需要在未来的iOS版本中关注这一点,但在iOS 10.1上运行良好 – Tys 2017-03-13 12:41:13

相关问题