gpt4 book ai didi

swift - TableView ReloadSection 崩溃了吗?

转载 作者:可可西里 更新时间:2023-11-01 01:58:42 27 4
gpt4 key购买 nike

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“尝试删除第 100 节,但更新前只有 7 个节”*** 首先抛出调用栈:

我正在尝试制作可折叠的 TableView WithAnimation。从 Header 中点击 View ,它将调用切换部分方法。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!)
}

最佳答案

我先重新加载了上一节,然后重新加载了更改后的部分,如下所示

 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!)
}

关于swift - TableView ReloadSection 崩溃了吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48474338/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com