繁体   English   中英

展开/折叠部分 tableview swift

[英]Expand/Collapse section tableview swift

我有 3 个部分及其相应的单元格如何使其展开/折叠? 我创建了一个扩展的结构:下面的 Bool 值是我的代码。 *这是我的 cellForRowAt *

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    switch (indexPath.section) {
    case 0:
        if let cell1 = tableview.dequeueReusableCell(withIdentifier: "TimeCardDetailedTableViewCell", for: indexPath) as? TimeCardDetailedTableViewCell{
            
        return cell1
        }
    case 1:
        if let cell2 = tableview.dequeueReusableCell(withIdentifier: "MessageDetailedTableViewCell", for: indexPath) as? MessageDetailedTableViewCell{
            
            
        return cell2
        }
    case 2:
        if let cell3 = tableview.dequeueReusableCell(withIdentifier: "CrewDetailedTableViewCell", for: indexPath) as? CrewDetailedTableViewCell{
            
        return cell3
        }
    default:
        return UITableViewCell()
    }
    return UITableViewCell()
}

--->>这是numberOfRowsInSection

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    switch (section) {
    case 0:
        return self.timecards.count
    case 1:
        return self.msglog.count
    default:
        return self.profile.count
    }
}

--->>> viewForHeaderInSection

 func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    if section == 0{
        let header1 = Bundle.main.loadNibNamed("TimeCardHeaderTableViewCell", owner: self, options: nil)?.last as! TimeCardHeaderTableViewCell
        header1.backgroundColor = .red
        return header1
    } else if section == 1 {
        let header2 = Bundle.main.loadNibNamed("MessageTableViewCell", owner: self, options: nil)?.last as! MessageTableViewCell
        return header2
    } else if section == 2 {
        let header3 = Bundle.main.loadNibNamed("CrewTableViewCell", owner: self, options: nil)?.last as! CrewTableViewCell
        return header3
    }
    return UITableViewCell()
    
}

如何在 iOS 新的 section.im 上实现扩展

一种方法是像这样更新您的numberOfRowsInSection

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
   switch (section) { 
     case 0: return self.hiddenSections.contains(0) ? 0 : self.timecards.count 
     case 1: return self.hiddenSections.contains(1) ? 0 : return self.msglog.count 
     case 2: return self.hiddenSections.contains(2) ? 0 : return self.profile.count 
   } 
}

viewForHeaderInSection 将是这样的,您将在其中设置tagselector

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let sectionButton = UIButton()
        sectionButton.setTitle(String(section),
                               for: .normal)
        sectionButton.backgroundColor = .systemBlue
        sectionButton.tag = section
        sectionButton.addTarget(self,
                                action: #selector(self.hideSection(sender:)),
                                for: .touchUpInside)

        return sectionButton
    }

hideSection方法将具有隐藏/显示您的部分的逻辑

@objc
    private func hideSection(sender: UIButton) {
        let section = sender.tag
        
        func indexPathsForSection() -> [IndexPath] {
            var indexPaths = [IndexPath]()
            
            for row in 0..<self.tableViewData[section].count {
                indexPaths.append(IndexPath(row: row,
                                            section: section))
            }
            
            return indexPaths
        }
        
        if self.hiddenSections.contains(section) {
            self.hiddenSections.remove(section)
            self.tableView.insertRows(at: indexPathsForSection(),
                                      with: .fade)
        } else {
            self.hiddenSections.insert(section)
            self.tableView.deleteRows(at: indexPathsForSection(),
                                      with: .fade)
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM