我现有的 TableViewController 在 Xcode 11 beta1 上崩溃

My Existing TableViewController is crashing on Xcode 11 beta1

我开发了一个 UITableViewController 屏幕。它在 Xcode 10.2 上工作正常但是。当我 运行 在 Xcode 11 beta 1 上时,它崩溃如下。

我没有找到发生了什么事。

在 ViewDidLoad

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.tableFooterView = UIView()
    plateNoPrefix.becomeFirstResponder() // static cell textfield in tableViewcell
}

Exception… Attempted to access the table view's visibleCells while they were in the process of being updated, which is not allowed

我在为 iOS 13.

提供支持时遇到了同样的问题

This is a new exception in iOS 13 that UITableView will raise in order to prevent and proactively alert you of a situation that would previously cause undefined behaviour and a variety of strange, seemingly unrelated, and hard-to-debug issues (including crashes).

What is happening here is that UITableView is in the middle of asking its dataSource to return a cell for each visible row and is configuring the properties of the returned cells so they can be displayed. And in the middle of this updating -- most likely inside a callback from the table view itself about a specific row such as tableView(_:cellForRowAt:) tableView(_:canEditRowAt:), etc -- your code is asking the table view to return the visible cells. This is obviously problematic, because UITableView is right in the middle of preparing those cells, so it cannot possibly return a meaningful answer.

The fix for this is to look at where you are calling visibleCells in the backtrace when this exception is raised, and then do one of two things:

选项 1:

Move the usage of visibleCells to a better place, so that you aren't asking for the visibleCells from someplace that is called during the process of creating/configuring/updating those same cells. A great place to ask for the visible cells is after the table view lays out, so for example if the table view is the view of a view controller you can use viewDidLayoutSubviews(), or in a subclass of UITableView do it after calling super.layoutSubviews().

选项 2:

Depending on what you're actually trying to do, you might be able to skip using visible cells altogether. For example, you might be able to leverage the callbacks tableView(_:willDisplay:forRowAt:) and tableView(_:didEndDisplaying:forRowAt:) to track when cells are visible instead.

If you are hitting this exception and you think you are requesting the visible cells from a location that should be valid/allowed, please share the backtrace when you hit this exception and details about what you're trying to do.

更新:

我确定,但是 plateNoPrefix.becomeFirstResponder() 导致了崩溃。截至目前,您可以通过将此代码粘贴到 viewDidAppear 方法

中进行检查

延迟后执行此代码(为我工作)

 DispatchQueue.main.asyncAfter(deadline: .now()+0.1) {
      // Your code
 }

详细说明可以参考Apple Developer Forum

这是 iOS13 中的一个新异常,UITableView 将引发该异常以防止并主动提醒您以前会导致未定义行为和各种奇怪的、看似无关,hard-to-debug 问题

请看Apple Developer Forum