【问题标题】:Expanding tableview sections on clicking each section单击每个部分时展开 tableview 部分
【发布时间】:2019-03-15 05:37:59
【问题描述】:

我有一个表格视图,它有 2 个部分和每个部分下方的一些单元格(可以是动态的)显示相关数据。

这是我为显示数据而编写的代码...

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
  }

现在我想要实现的是,如果我点击第一部分,它应该展开并显示它包含的单元格,第二个单元格也应该发生同样的情况。再次单击每个部分应隐藏已展开的单元格。

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

【问题讨论】:

标签: ios swift uitableview


【解决方案1】:

添加一个数组来跟踪部分的展开/折叠

let sectionStats = [Bool](repeating: true, count: 2)

添加一个IBAction来跟踪section tap,更新对应section的sectionStats值并重新加载section

并更新您的numberOfRowsInSection,如下所示

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

关于如何使用可折叠部分构建表格视图的好教程: https://medium.com/ios-os-x-development/ios-how-to-build-a-table-view-with-collapsible-sections-96badf3387d0

【讨论】:

  • 但是如何在默认部分设置 IBAction..?
  • @user308123,您可以使用自定义标题。
  • 谢谢@Satish...我会尝试...:)
  • 它确实有效@Satish。非常感谢...!但我想让那个按钮出现在该部分的中间,使其位于该部分的最左侧。另外,您介意在 numberOfRowsInSection guard sectionStats[section] else { return 0 } 中解释这一行的作用吗
  • button.contentHorizontalAlignment = .left 左对齐按钮内容。如果为 collaspe 状态 sectionStats[section] == false,这将返回 0
【解决方案2】:

这种功能需要更多代码,我无法在这里编写整个代码,但我可以向您解释实现此目的的概念,并将附上一些我用来最终创建类似功能的优秀教程这个


  1. 首先您需要为您的部分创建一个自定义HeaderView
  2. 接下来,您需要在您的部分上添加一个UITapGestureRecognizer,并且需要在actionUITapGestureRecognizer 构造函数的一部分中提供的函数 中写入您的登录信息
  1. 您需要在您的HeaderView 文件中创建一个单独的Protocol,并且您的包含您的ViewControllerTableView 将采用该协议并处理是否展开或折叠您的行
  1. 此外,您需要为每个部分创建一个单独的Struct 实例,其中将包含一个boolean variable,用于指示该部分是展开还是折叠

这是在 iOS 中创建 Expandable List 时需要的基本概念。

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

Tutorial 1

Tutorial 2

Tutorial 3

Tutorial 4

【讨论】:

  • 感谢@Shubham Bakshi 的回答...:)
猜你喜欢
  • 1970-01-01
  • 2022-11-24
  • 2023-03-20
  • 1970-01-01
  • 1970-01-01
  • 2017-06-05
  • 2020-08-30
  • 2019-11-29
  • 1970-01-01
相关资源
最近更新 更多