我在使用 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)
}
有人遇到过类似的问题并且可能知道如何解决这个问题吗? 我已经为 apple 创建了一个雷达。
最佳答案
我遇到了类似的问题,并尝试通过延迟分派(dispatch)到主线程来解决它,并不断减少时间以查看它是否有意义。我最终得到了这个:
DispatchQueue.main.async(execute: {
self.tableView.setEditing(false, animated: true)
})
解决了这个问题,虽然你之后有一点奇怪的删除动画,大概是由于 tableView 的切换状态和 iOS 11 SDK 中的一些内部竞争条件。
关于iOS 11 UITableView isEditing 标志设置不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46345248/