ios - 单击每个部分即可展开表格 View 部分

标签 ios swift uitableview

我有一个表格 View ,其中有 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
  }

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

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

最佳答案

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

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

添加一个 IBAction 来跟踪部分点击,更新相应部分的 sectionStats 值并重新加载部分

并更新您的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()
}

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

关于ios - 单击每个部分即可展开表格 View 部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52738014/

相关文章:

c# - Unity XMPP 聊天

当我按下主页按钮然后恢复它时,ios7 应用程序崩溃

xcode - 追加数组时要字符串的任何对象

ios - 我应该在 cellForItemAtIndexPath 中设置 cell.bound/cell.frame 吗?

ios - 返回 NULL 的 tableView 单元格

ios - Swift:如何使 UITableViewCell 高度适应其中的 UITextView

ios - HTML/CSS/JS 是否可以通过 Adob​​e Air 编译来构建移动应用程序

cocoa - 新的 "iOS data protection APIs"是什么?

ios - NSAttributedString 中有两个空格,我的程序崩溃了

swift - `guard let foo = foo` 什么时候合法的?