我有一个UITableView
,它可以间接访问其数据(即,它由指针的数据结构支持),并且每当从远程服务器中提取新数据时,都会异步地重新加载其节。看来我的重装计划有竞争条件;即使我只在主线程上执行表视图重装,我偶尔也会看到以下错误:
由于未捕获的异常“ NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:第1节中的行数无效。更新(1)之后现有节中包含的行数必须等于行数该部分包含在更新(0)之前,加上或减去从该部分插入或删除的行数(已插入0,已删除0),以及加上或减去移入或移出该部分的行数(移入0) ,0移出)。'
我的代码结构如下:
var table_data_indirect_1: [CustomDataPointer] = []
var table_data_indirect_2: [CustomDataPointer] = []
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let x = customData(in: indexPath.section)[indexPath.row], let cell = table.dequeueReusableCell(withIdentifier: "custom-identifier", for: indexPath) as? CustomCell {
cell.setCustomData(to: x)
return cell
} else {
return UITableViewCell()
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return customData(in: section).count
}
func numberOfSections(in: UITableView) -> Int {
return 2
}
func customData(in section: Int) -> [CustomDataStruct] {
switch section {
case 0:
return table_data_indirect_1.map { asyncLoadCustomData($0, for: section) }
case 1:
return table_data_indirect_2.map { asyncLoadCustomData($0, for: section) }
default:
return []
}
}
func asyncLoadCustomData(_ customDataPointer: CustomDataPointer, for section: Int) -> CustomDataStruct {
if customDataPointer.isLoaded {
return customDataPointer.backingStruct
} else {
customDataPointer.asyncLoad { result in
DispatchQueue.main.async { [weak self] in
let section_set = IndexSet(integersIn: section...section)
self?.table.reloadSections(section_set, with: .none)
}
}
return customDataPointer.placeholderStruct
}
}