【问题标题】:showing the Rows when a Section header is selected选择部分标题时显示行
【发布时间】:2025-12-04 21:05:01
【问题描述】:

我有一个包含表格视图的视图控制器。我想添加在按下节标题时显示 ROW 的功能,反之亦然(可扩展表格视图)。

下面的代码显示了我为表格视图编写的代码:

  func numberOfSections(in tableView: UITableView) -> Int {
        return data.count
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let cell = tableview.dequeueReusableCell(withIdentifier: "ReportHeaderCell") as! ReportHeaderTableViewCell
        cell.IMG.image = #imageLiteral(resourceName: "Cost_Arrow")
        cell.BillHeaderTitle.text = data[section].billType
        cell.totalValue.text = data[section].totalValue?.addComma
        cell.dateLbl.text = data[section].billDate

        let animationView = LOTAnimationView(name: "CloseOpen")
        animationView.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
        animationView.loopAnimation = false
        animationView.autoReverseAnimation = false
        animationView.play(fromProgress: 0, toProgress: 0.0, withCompletion: nil)
        cell.openCloseView.addSubview(animationView)
        animationView.play()

        cell.MainView.addTapGesture {
            print("header is tapped")
            if self.isOpen[section] == true
            {
                print("header is open")
                self.isOpen[section] = false
                // we'll try to close the section first by deleting the rows
                var indexPaths = [IndexPath]()
                for row in self.data[section].units.indices {
                    print(0, row)
                    let indexPath = IndexPath(row: row, section: section)
                    indexPaths.append(indexPath)


//                    tableView.deleteRows(at: indexPaths, with: .bottom)
//                    tableview.reloadData()
                    self.tableview.deleteRows(at: indexPaths, with: .top)


                }
                let animationView = LOTAnimationView(name: "CloseOpen")
                animationView.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
                animationView.loopAnimation = false
                animationView.autoReverseAnimation = false
                animationView.animationSpeed = 2.5
                animationView.play(fromProgress: 1, toProgress: 0.0, withCompletion: nil)
                cell.openCloseView.addSubview(animationView)
                animationView.play()
            }
            else
            {
                print("header is close")

                self.isOpen[section] = true
                //let's open it
                // we'll try to close the section first by deleting the rows
                var indexPaths = [IndexPath]()
                for row in self.data[section].units.indices
                {
                    print(0, row)
                    let indexPath = IndexPath(row: row, section: section)
                    indexPaths.append(indexPath)

//                    tableView.insertRows(at: indexPaths, with: .top)
                    self.tableview.insertRows(at: indexPaths, with: .top)

//                    tableview.reloadData()
                    let animationView = LOTAnimationView(name: "CloseOpen")
                    animationView.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
                    animationView.loopAnimation = false
                    animationView.autoReverseAnimation = false
                    animationView.play(fromProgress: 0, toProgress: 1, withCompletion: nil)
                    animationView.animationSpeed = 2.5
                    cell.openCloseView.addSubview(animationView)
                    animationView.play()
                }
        }


        }
        return cell

    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if self.isOpen[section] == true
        {
        print(data[section].units.count)
            return data[section].units.count
        }
        else
        {
            return 0
        }
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableview.dequeueReusableCell(withIdentifier: "ReportCell") as! ReportTableViewCell
//
//        if indexPath.row != 0
//        {
        cell.UnitName.text = data[indexPath.section].units[indexPath.row]?.unitName
//            print(data[indexPath.section].units[indexPath.row]?.unitName!)
        cell.Unitdebit.text = data[indexPath.section].units[indexPath.row]?.debit?.addComma
            if indexPath.row == data.count - 1
            {
                cell.DownRightView.isHidden = true
            }

//        }
//        else{}

        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if isOpen[indexPath.section] == true
        {
            self.isOpen[indexPath.section] = false
            let section = IndexSet.init(integer: indexPath.section)
            tableView.reloadSections(section, with: .top)
        }
        else
        {
            self.isOpen[indexPath.section] = true
            let section = IndexSet.init(integer: indexPath.section)
            tableView.reloadSections(section, with: .bottom)
        }
    }


    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 90
    }

已解决: 这里有一个很大的问题!!这是:

例如,我们的表格视图中有四个部分,用户选择数字 2。一旦用户选择数字 2,部分数字 3 和 4 就会消失!!!

澄清一下,所选部分数量较多的部分将消失!

有人知道我在这里做错了什么吗??

这个错误仍然存​​在: 谁能帮忙!!!我卡住了

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:第 1 节中的行数无效。更新 (2) 后现有节中包含的行数必须等于行数更新前包含在该节中的行数 (0),加上或减去从该节插入或删除的行数(1 插入,0 删除),加上或减去移入或移出该节的行数(0 移入, 0 移出)。

错误相关的json

    [
  {
    "totalValue" : 11,
    "units" : [
      {
        "unitName" : "jack",
        "debit" : 11,
        "unit" : 1
      },
      {
        "unitName" : "jack1",
        "debit" : 11,
        "unit" : 2
      }
    ],
    "billType" : "glocery",
    "billDate" : "1998\/08\/20",
    "billId" : 29049
  }

【问题讨论】:

  • 不要删除要关闭的单元格,不要添加要展开的单元格。相反,只需将它们的高度设置为 0 并在您点击标题时重新加载单个部分。
  • tableView(viewForHeaderInSection:) 的末尾而不是return cell 输入return cell.contentView。我不知道为什么,但相信我,它会起作用的
  • @SepehrBehroozi 似乎没问题,但有问题。 1.它随机崩溃应用程序并显示登录建议我刚刚将错误添加到问题
  • @SepehrBehroozi 我还添加了与错误相关的 JSON。当 json 有多个单位时,我认为它会出现此错误

标签: ios swift uitableview expandable-table


【解决方案1】:

将插入和删除放在应该解决问题的for循环之外

【讨论】:

    最近更新 更多