[英]Tableview crash in height for row At indexPath
我正在實現一個帶有 2 個 xib 文件的表格視圖。 但在某些設備中,rowAt 的高度崩潰:function(根據崩潰日志)下面是我的代碼
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == 0 {
return 340.0
} else {
return (contentHeights[indexPath.row] + 70)
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: “customId1”, for: indexPath) as? customCell1
if let details = Details{
print(detail)
}
cell?.selectionStyle = .none
if cell == nil {
print(“nil cell”)
}
return cell!
} else {
let cell = tableView.dequeueReusableCell(withIdentifier:customId2, for: indexPath) as? customCell2
cell?.selectionStyle = .none
if cell == nil {
print(“nil cell”)
}
return cell!
}
}
我在這個 cellforRow 中填充 wkweview 內容,然后在設置 tableview 內容時重新加載特定行。
盡管它在所有設備上都運行良好,但在 350 台設備中的 2 台設備上崩潰。
知道為什么會這樣嗎?
您確定表視圖的 state ( dataSource
)與您的實際數據(而不是dataSource
)同步嗎?
我從您的代碼中看到的是行高的計算涉及數組項訪問。 通過調用必要的方法重新加載表視圖 state(例如beginUpdates
- endUpdates
和reloadData
方法),確保表視圖的數據完全同步。
此外,請務必在您的cellForRowAt
方法中使用assert
離子。 它對於開發目的很有用,並且可以避免使用繼續執行代碼並可能導致未定義行為的print
語句。
通過這樣做,您可以跳過錯誤,但檢查單元格為何來到 null。 並檢查 heightforRowAt contentHeights 是否具有該索引
if cell == nil {
print(“nil cell”)
return UITableViewCell()
}
return cell!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.