【问题标题】:iOS 11 UITableView isEditing flag not set correctlyiOS 11 UITableView isEditing 标志未正确设置
【发布时间】:2017-09-21 13:27:55
【问题描述】:

我在使用 iOS 11 SDK 时遇到了一个非常奇怪的问题。 在使用滑动手势删除单元格后,在 iOS 11 设备和模拟器上将 UITableView 的 editing 标志设置为 false 时,设置后的下一行仍保持为 true。 在 iOS 10 设备和低于标志的情况下,将 correclty 设置为 false。 请看一下这个简短的例子。

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return true
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell!
    return cell
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        tableView.deleteRows(at: [indexPath], with: .fade)
        endEditingMode()
    }
}

func endEditingMode() {
    tableView.setEditing(false, animated: true)

    // Here we expect `isEditing = false` since it is set the line before but it is still `true` on iOS 11 devices
    // On <= iOS 10 devices however its `false`
    print(tableView.isEditing)
}

任何人遇到类似的问题,可能知道如何解决这个问题? 我已经为苹果创建了一个雷达。

【问题讨论】:

  • 这是 UITableViewController 还是添加到 UIViewController 的表视图?表格最初是如何设置为编辑模式的?
  • 它是一个添加到 UIViewController 的 tableView
  • 还有,表格最初是如何设置为编辑模式的?
  • 抱歉错过了那个。当我们使用滑动删除功能时,编辑设置为 true。
  • 嗯。如果你在setEditing 之后将一个块分派到主队列并检查块内的tableView.isEditing 怎么办?我的假设是表格视图结束编辑,只是不同步。

标签: ios swift uitableview swift3 ios11


【解决方案1】:

我遇到了类似的问题,并尝试通过延迟调度到主线程来解决它,并继续减少时间以查看它是否有意义。我最终得到了这个:

DispatchQueue.main.async(execute: {
   self.tableView.setEditing(false, animated: true)
})

解决了这个问题,尽管之后你有一些奇怪的删除动画,可能是由于 tableView 的切换状态和 iOS 11 SDK 中的一些内部竞争条件。

【讨论】:

    【解决方案2】:

    尝试将 UISwipeActionsConfiguration(iOS 11 API) 与 UIContextualAction 一起使用。执行操作后,您应该从 UIContextualActionHandler 调用 block。此块触发关闭滑动动画。然后使用带有完成块的 CATransaction 将 UITableView 编辑设置为 NO。

    [CATransaction begin];
    [CATransaction setCompletionBlock:^{
       [self.tableView setEditing:NO animated:NO];
    }];
    completionHandler(YES); // From UIContextualAction
    [CATransaction commit];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-04
      • 1970-01-01
      • 2018-01-24
      • 2017-12-09
      • 2021-07-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多