gpt4 book ai didi

IOS swift 4展开和折叠tableview不工作并导致崩溃

转载 作者:行者123 更新时间:2023-11-28 07:27:21 25 4
gpt4 key购买 nike

以下是我添加节标题 View 单元格的代码

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cell = tableView.dequeueReusableCell(withIdentifier: "menuHeaderTableViewCellID") as! MenuHeaderTableViewCell
cell.foodMenuItems = menuResult?.foodMenuItems?[section]
cell.setParentUI()
cell.expandCollapseClicked = {
[weak self]
(postiton) in
let isCollapsed = self?.menuResult?.foodMenuItems?[postiton].isCollapsed ?? false
self?.menuResult?.foodMenuItems?[postiton].isCollapsed = !isCollapsed
self?.tableViewMenu?.beginUpdates()
self?.tableViewMenu?.reloadSections([section], with: .fade)
self?.tableViewMenu?.endUpdates()

}
return cell
}

以下是每行每节的计数代码

 func numberOfSections(in tableView: UITableView) -> Int {
return (menuResult?.foodMenuItems?.count) ?? 0
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let rowCount = menuResult?.foodMenuItems?[section].items?.count ?? 0
let isCollpased = menuResult?.foodMenuItems?[section].isCollapsed ?? false
return isCollpased ? 0 : rowCount
}

以下是标题 View 单元格中的代码

@IBAction func expandCollapseClicked(_ sender: UIButton) {
guard let superView = self.superview as? UITableView else {
return
}
expandCollapseClicked?(superView.indexPath(for: self)?.section ?? 0)
}

我在折叠第一部分时遇到问题,我的标题消失了,当我尝试折叠其他部分时出现以下异常如何解决?

 *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (0) must be equal to the number of rows contained in that section before the update (25), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
*** First throw call stack:

最佳答案

问题可能出在:

  1. 避免dequeueReusableCell。尝试使用带标签和按钮的 UIView。
  2. expandCollapseClicked = {...}应该由 UIButton 调用。因为它的发件人是 UIButton。尝试使用:

    view.button.expandCollapseClicked = {...}

希望这会奏效。

关于IOS swift 4展开和折叠tableview不工作并导致崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56110705/

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