在不处于编辑模式时重新排序UITableView上的控件?

Sha*_*des 13 iphone uitableview ios4

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

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

jrc*_*jrc 20

我找到了解决方案.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)
    }

}
Run Code Online (Sandbox Code Playgroud)

现在只需设置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
}
Run Code Online (Sandbox Code Playgroud)

唯一的缺点是这与表视图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
}
Run Code Online (Sandbox Code Playgroud)

此外,在-viewDidLoad或您的故事板中,将初始值设置allowsMultipleSelectionDuringEditing为false.

  • 这在iOS 13中不再起作用。在Beta 1和Beta 2上进行了测试 (2认同)
  • iOS 13 有修复吗? (2认同)

Xin*_*ing 9

重新排序没有删除,没有缩进.

- (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;
}
Run Code Online (Sandbox Code Playgroud)

参考:Stefan von Chossy 在这里发布的解决方案


mac*_*oss 8

据我所知,你只能使用编辑标志来做到这一点.但是,实现类似的效果并不难.

有自己的布尔值即deleting.将tableview单元格设置为cell.showsReorderControl = YES.然后实现移动单元格所需的所有方法.我认为你正在寻找的代码是. – tableView:editingStyleForRowAtIndexPath: 如果deleting == YES返回,UITableViewCellEditingStyleDelete否则返回UITableViewCellEditingStyleNone.最后,tableView.editing = YES始终设置.如果在更换deletingbool 之前和之后你运行[tableView beginUpdates],[tableView endUpdates]一切都应该工作正常.如果这没有意义,请告诉我,我会再试一次.

  • 顺便说一下,这将停止滑动删除. (6认同)