Ios indexPath处行的Tableview高度崩溃
我正在用2个xib文件实现一个tableview。 但在一些设备中,rowAt的高度会崩溃:根据崩溃日志运行 下面是我的代码Ios indexPath处行的Tableview高度崩溃,ios,uitableview,crash,heightforrowatindexpath,Ios,Uitableview,Crash,Heightforrowatindexpath,我正在用2个xib文件实现一个tableview。 但在一些设备中,rowAt的高度会崩溃:根据崩溃日志运行 下面是我的代码 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { if indexPath.row == 0 { return 340.0 } else { return
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台设备崩溃
知道为什么会发生这种情况吗?通过这样做,您可以跳过错误,但可以检查单元格为什么变为空。还可以检查heightforrow和contentHeights是否具有该索引
if cell == nil {
print(“nil cell”)
return UITableViewCell()
}
return cell!
是否确定表视图的数据源状态与实际数据同步,而不是与数据源同步 我从您的代码中看到,行高度的计算涉及数组项访问。通过调用必要的方法重新加载表视图状态(如BeginUpdate EndUpdate和reloadData方法),确保表视图的数据完全同步
另外,在cellForRowAt方法中使用断言。它对于开发目的很有用,并且避免使用打印语句继续执行代码,并可能导致未定义的行为。这样做可以跳过错误,但可以检查单元格变为空的原因。如果单元格==nil{print“nil cell”return UITableViewCell}返回单元格,还可以检查heightforrow在contentHeights中是否具有该索引!谢谢你的帮助,从现在起我将使用assert。谈到arrayItem,我将在2XIB中实现webview,其中数据将填充到webview中,基于内容,我将计算高度并替换数组中的值。我已检查rowForHeight中的数组未引发nul指针异常