簡體   English   中英

Xcode 11 tableview 在啟動時崩潰

[英]Xcode 11 tableview crashes on launch

啟動一個使用 tableview 的應用程序,模擬器在啟動時崩潰,有:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Attempted to call -cellForRowAtIndexPath: on the table view while it was in the process of updating its visible cells, which is not allowed.

相同的代碼在 iOS 12 / Xcode 10.2 中有效。

注釋掉下面突出顯示的函數調用解決了崩潰:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    updateRowHeight(indexPath: indexPath) // CRASH IS BECAUSE OF CALLING THIS HERE
    return myTableView.frame.height / CGFloat(CellsDataEnum.allCases.count)
}


func updateRowHeight(indexPath: IndexPath) { // WHICH CALLS THIS
     myTableView.cellForRow(at: indexPath)?.textLabel?.font = UIFont.systemFont(ofSize: myTableView.frame.height / 10)
}

相反,我現在在cellForRowAt設置字體:

cell.textLabel?.font = UIFont.systemFont(ofSize: myTableView.frame.height / 10)

為什么第一個版本在 iOS 12 中可以運行,但在 iOS 13 中崩潰?

(蘋果通過反饋助手)

  • 此異常是 iOS 13 中的新增功能,如果您在表格視圖正在更新其單元格的過程中嘗試要求表格視圖返回單元格,則會發生此異常。
  • 您可以在此處的開發者論壇上了解更多相關信息: https : //forums.developer.apple.com/thread/117537#364166

無需從heightForRowAt調用updateRowHeight 事實上,應該刪除整個updateRowHeight方法。 cellForRowAt設置字體。

鑒於您的heightForRowAt為所有行返回相同的值,也刪除該方法。 這是非常低效的。 只需將 table view 的rowHeight屬性設置為所需的高度。

這些更改應該適用於所有 iOS 版本,並且效率也會更高。

iOS 13 可能存在問題,因為您正在更新表視圖時嘗試訪問單元格。 它可能在之前的版本中有效,但從heightForRowAt訪問cellForRowAt從來都不是一個好主意。

在我的情況下非常相似的問題,通過避免這一行來解決:

 table.numberOfRows(inSection: cells.count)

這種在更新單元格的過程中要求表格視圖返回單元格數據的嘗試會導致崩潰。 刪除該行后,該表工作正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM