【发布时间】:2018-03-12 10:03:52
【问题描述】:
我正在尝试重新加载我的 TableView,但我收到此异常“由于未捕获的异常 'NSInternalInconsistencyException' 而终止应用程序,原因:'尝试从第 1 节中删除第 3 行,它在更新前仅包含 0 行'”。
下面是我的代码:-
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if statusTableView == tableView {
return ModelAssessStatus.sharedInstance.arrType.count
}
else {
if !sections[section].expanded {
return 0
}
else {
return sections[section].names.count
}
//return sections[section].names.count
/*if sections[section].expanded == true {
return sections[section].names.count
}
else {
return 0
}*/
}
}.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var expandableCategoryCell = tableView.dequeueReusableCell(withIdentifier: "ExpandableCategoryCell") as? ExpandableCategoryCell
if expandableCategoryCell == nil {
var nib:Array = Bundle.main.loadNibNamed("ExpandableCategoryCell", owner: self, options: nil)!
expandableCategoryCell = nib[0] as? ExpandableCategoryCell
}
expandableCategoryCell?.selectionStyle = .none
expandableCategoryCell?.labelName.text = sections[indexPath.section].names[indexPath.row]
return expandableCategoryCell!
//return UITableViewCell()
}.
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if tableView == self.expandableTableView {
return 60
}
return 0
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
if tableView == self.expandableTableView {
return 0
}
return 0
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if tableView == expandableTableView {
let cell = self.expandableTableView.dequeueReusableHeaderFooterView(withIdentifier: "ExpandableHeaderView")
let header = cell as! ExpandableHeaderView
header.customInit(title: sections[section].categoryType, section: section, delegate: self, isSelected: sections[section].fullSelected)
if sections[section].expanded {
header.imgArrow?.image = UIImage(named : "down_Arow")//.transform = (header.imgArrow?.transform.rotated(by: CGFloat(Double.pi)))!
}
else {
header.imgArrow?.image = UIImage(named : "error")
}
return cell
}
else {
return UIView.init(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
}
}
//Height For Raw at indexPath
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if statusTableView == tableView {
return 50
}
else {
if (sections[indexPath.section].expanded) {
return 60
}
else {
return 0
}
}
}
}
extension PeerCategoryStatusVC : expandableHeaderViewDelegate {
func toggleSections(header: ExpandableHeaderView, section: Int) {
sections[section].expanded = !sections[section].expanded
//expandableTableView.reloadData()
expandableTableView.beginUpdates()
for index in 0..<sections[section].names.count {
expandableTableView.reloadRows(at: [IndexPath(row : index, section : section)], with: .automatic)
}
expandableTableView.endUpdates()
//self.view.layoutIfNeeded()
}
}.
从这里开始更新和结束更新 TableView。 我正在尝试重新加载 tableView 但不知道为什么会出现异常。
如果我保持相同的行数,那么约束问题就来了。
有什么要补充的吗?
【问题讨论】:
-
因为您正在更改
numberOfRows(inSection:)返回的行数,所以一旦您更改相关部分的展开状态,必要的行就会“自动”插入或删除。您只需要重新加载该部分。现在,当您折叠一个部分时,该部分中的行数变为 0,因此当您尝试重新加载该部分中的一行时,您会遇到异常。 -
哦,你的意思是我只需要重新加载部分?
-
是的,尝试一下,如果这不能帮助指出代码中的哪一行引发了异常
-
OK 让我检查一下。
-
由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“尝试从第 1 节中删除第 0 行,更新前仅包含 0 行”
标签: ios swift tableview height