Ios 单击每个部分后展开tableview部分

Ios 单击每个部分后展开tableview部分,ios,swift,uitableview,Ios,Swift,Uitableview,我有一个tableview,它有两个部分,每个部分下面有一些单元格(可以是动态的),显示相关数据 这是我用来显示数据的代码 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if section == 0 { return recentUsers?.count } else { return groupNameArray?.co

我有一个tableview,它有两个部分,每个部分下面有一些单元格(可以是动态的),显示相关数据

这是我用来显示数据的代码

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if section == 0 {
      return recentUsers?.count
    } else {
      return groupNameArray?.count
    }

  }


func numberOfSections(in tableView: UITableView) -> Int {
     return 2
}




 func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    if section == 0 {
      return "  CHAT LIST"
    } else {
      return "  GROUPS"
    }
  }




 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! RecentMessageCell
    cell.tag = indexPath.row

    if indexPath.section == 0 {

        if let user = recentChatUsers?[indexPath.row] {
          cell.idLabel?.text = user.id

      }
    } else {


      if groupNameArray.isEmpty == false {
       let grpArr = groupNameArray[indexPath.row]
       cell.userNameLabel?.text = grpArr.grpname
      }
    }

    return cell
  }
现在我想要实现的是,如果我点击第一部分,它应该展开并显示它包含的单元格,第二个单元格也应该如此。再次单击这些部分中的每一部分都会隐藏展开的单元格


我确实在互联网上搜索解决方案。但是,尽管有可用的资源,我找不到太多的帮助来解决我的问题

添加一个数组以跟踪区段扩展/折叠

let sectionStats = [Bool](repeating: true, count: 2)
添加一个iAction以跟踪区段点击,更新相应区段的
sectionStats
值并重新加载区段

并更新您的
行数部分
,如下所示

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        guard sectionStats[section] else {
            return 0
        }

        if section == 0 {
            return 1
        } else {
            return list.count
        }
    }
可攻丝割台:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    return headerView(section: section, title: section == 0  ? "CHAT LIST" : "GROUPS")
}

private func headerView(section: Int, title: String) -> UIView {
    let button = UIButton(frame: CGRect.zero)
    button.tag = section
    button.setTitle(title, for: .normal)
    button.setTitleColor(UIColor.red, for: .normal)
    button.addTarget(self, action: #selector(sectionHeaderTapped), for: .touchUpInside)

    return button
}

@objc private func sectionHeaderTapped(sender: UIButton) {
    let section = sender.tag
    sectionStats[section] = !sectionStats[section]
    tableView.beginUpdates()
    tableView.reloadSections([section], with: .automatic)
    tableView.endUpdates()
}
关于如何构建具有可折叠部分的表视图的良好教程:

这类功能需要更多的代码,我无法在这里编写全部代码,但我可以向您解释实现这一功能所使用的概念,并附上一些好的教程,这些教程是我用来最终创建类似功能的


  • 首先,您需要为您的分区创建一个自定义的
    HeaderView
  • 接下来,您需要在分区上安装一个
    uitagesturerecognizer
    ,并需要在
    uitagesturerecognizer
    构造函数的
    action
    部分中提供的函数中写入您的登录名
  • 您需要在
    HeaderView
    文件中创建一个单独的
    协议
    ,包含
    TableView
    ViewController
    将采用该协议,并将处理是否展开或折叠行
  • 此外,还需要为每个节创建一个单独的
    Struct
    实例,该实例将包含一个
    布尔变量
    ,该变量将指示该节是展开还是折叠
  • 这是在iOS中创建
    可扩展列表时需要的基本概念

    下面我附上了一些教程的链接:


    您可以按照此示例操作。不,它不是第三方。只需查看
    ExpandableTableViewExample
    的实现,然后自己动手就可以了。。因此删除了我的评论…:)您可以按照此操作,但我如何在默认节上设置IBAction…?@user308123,您可以使用自定义标题。谢谢@Satish…我将尝试…:)它确实有效@Satish。谢谢。。。!但我希望这个按钮出现在部分的中间,在这个部分的最左边。另外,请您解释一下这一行在numberOfRowsInSection
    guard sectionStats[section]中的作用,或者{return 0}
    button.contentHorizontalAlignment=.left
    将按钮内容向左对齐。如果collaspe state
    sectionStats[section]==false,则返回0(谢谢@Shubham Bakshi…)