iOS11 UITableView isEditing 标志设置不正确

iOS 11 UITableView isEditing flag not set correctly

我在使用 iOS 11 SDK 时遇到了一个非常奇怪的问题。 使用滑动手势删除单元格后,在 iOS 11 台设备和模拟器上将 UITableView 的 editing 标志设置为 false 时,设置后仍停留在下一行 true . 在 iOS 10 台设备上,该标志被正确设置为 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)
}

有没有人遇到过类似的问题并且可能知道如何解决这个问题? 我已经为苹果制作了一个雷达。

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

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

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

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

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