TableView ReloadSection 崩溃了?
Posted
技术标签:
【中文标题】TableView ReloadSection 崩溃了?【英文标题】:TableView ReloadSection Get Crashed? 【发布时间】:2018-01-27 09:17:42 【问题描述】:由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“尝试删除第 100 节,但更新前只有 7 个节” *** 首先抛出调用栈:
我正在尝试使用动画制作可折叠的 TableView。From Header 确实点击了视图,它将调用切换部分方法。Ima 的工作基于它是否被选中或没有 bool 值。
class ExploreLocallyVc: UIViewController
var headerViewMain = HeaderView()
@IBOutlet weak var tableView: UITableView?
var ArrayOfCollapsable : [Int:Bool]? = [:]
var prevIousSelection : Int? = 100
override func viewDidLoad()
super.viewDidLoad()
tableView?.estimatedRowHeight = 80
tableView?.rowHeight = UITableViewAutomaticDimension
tableView?.sectionHeaderHeight = 60
tableView?.separatorStyle = .none
extension ExploreLocallyVc:UITableViewDelegate,UITableViewDataSource
func numberOfSections(in tableView: UITableView) -> Int
return 7
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
if (ArrayOfCollapsable?[section] == true)
print("yes")
return 4
else
print("No")
return 0
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
let cell = tableView.dequeueReusableCell(withIdentifier: "q", for: indexPath)
cell.textLabel?.text = "\(indexPath.row)"
return cell
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
headerViewMain = (Bundle.main.loadNibNamed("HeaderView", owner: self, options: nil)![0] as? HeaderView)!
headerViewMain.titleLabel?.text = "\(section)"
if (ArrayOfCollapsable?[section] == true)
headerViewMain.arrowLabel?.transform = CGAffineTransform(rotationAngle: CGFloat.pi)
headerViewMain.section = section
headerViewMain.delegate = self
return headerViewMain
extension ExploreLocallyVc:HeaderViewDelegate
func toggleSection(header: HeaderView, section: Int)
if (ArrayOfCollapsable?[section] == true)
print("yes")
ArrayOfCollapsable?[section] = false
else
print("No")
ArrayOfCollapsable?[section] = true
ArrayOfCollapsable?.updateValue(false, forKey: prevIousSelection!)
tableView?.reloadSections(NSIndexSet(index: section) as IndexSet, with: .automatic)
prevIousSelection = section
print(ArrayOfCollapsable!)
【问题讨论】:
"Attempt to delete row from section 1, but there are only 1 sections before update"的可能重复 【参考方案1】:我先重新加载了上一节,然后重新加载了更改的部分,如下所示
func toggleSection(header: HeaderView, section: Int)
if (ArrayOfCollapsable?[section] == true)
print("yes")
ArrayOfCollapsable?[section] = false
else
print("No")
ArrayOfCollapsable?.updateValue(false, forKey: prevIousSelection!)
if (tableView?.numberOfSections)! >= prevIousSelection!
tableView?.reloadSections(NSIndexSet(index: prevIousSelection!) as IndexSet, with: .automatic)
ArrayOfCollapsable?[section] = true
tableView?.reloadSections(NSIndexSet(index: section) as IndexSet, with: .automatic)
prevIousSelection = section
print(ArrayOfCollapsable!)
【讨论】:
以上是关于TableView ReloadSection 崩溃了?的主要内容,如果未能解决你的问题,请参考以下文章