如何在表视图单元格中处理全部打开/全部关闭,每个单元格在 iOS Swift 中都有查看更多/查看更少选项

问题描述

我正在做 Swift 项目,每个单元格都有查看更多/查看更少选项。我已经用以下代码做到了。它用于查看更多/查看更少选项。

//视图控制器类

var expanded:[IndexPath] = []

//Custom protocol

    func viewMoreTapped(cell: tableviewcell) {
        
        let indexpath = EntriesTableView.indexPath(for: cell)
        let indexPath = IndexPath(item: indexpath!.row,section: indexpath!.section)
        if(expanded.contains(indexPath)) {
            expanded.removeAll { (checkPath) -> Bool in
                return checkPath == indexPath
            }
        } else {
            expanded.append(indexPath)
        }
        entriesTableView.beginUpdates()
        entriesTableView.reloadRows(at: [indexPath],with: .none)
        entriesTableView.endUpdates()
    }

@IBAction func expandAllBtnAction(_ sender: Any) {
    
    isExpandedAll = !isExpandedAll
    expandAllButton.titlelabel.text = isExpandedAll ? "Open All" : "Close All"
    self.entriesTableView.reloadData()
}


        func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                 let cell = tableView.dequeueReusableCell(withIdentifier:"cellIdentifier",for:
         indexPath) as! MyTableViewCell
        let checkPath = IndexPath(row:indexPath.row,section: indexPath.section)
        if expanded.contains(checkPath) {
            cell.cellConfigure(index: indexPath.row,isexpanded: true,info: Info,expandedAll: isExpandedAll)
        } else {
            cell.cellConfigure(index: indexPath.row,isexpanded: false,expandedAll: isExpandedAll)
        }

    return cell

}

//Tableview cell class

    var isExpanded: Bool = false

    func cellConfigure(index: Int,isexpanded : Bool,expandedAll: Bool) {

//For open all/close all handling
      if expandedAll && isExpanded == false {
            isExpanded = true
            viewMoreBtn.titleLabel?.text = "View Less"
        } else  {
            viewMoreBtn.titleLabel?.text = isExpanded ? "View Less" : "View More"
        }
  //view more/view less handling               cellHeight.constant = isExpanded ? 40 : 120

}

    @IBAction func viewMoreBtnAction(_ sender: Any) {
        
        isExpanded = !isExpanded
        delegate?.viewMoreTapped(cell: self) // this is custom protocol
    }

每个单元格都运行良好查看更多/查看更少,但是在用户点击全部打开后,如果用户有,它应该打开所有单元格和每个单元格点击查看较少,它应该只关闭选定的单元格。但是,那个时候它不起作用。

要求是

  1. 如果用户点击查看更多,它应该是打开选定的单元格。如果用户点击查看较少,它应该关闭选定的单元格。
  2. 此外,如果用户点击全部打开,所有单元格都应该展开,同时如果用户点击特定单元格的视图较少,那么此时应该只关闭选定的单元格。
  3. 如果用户点击全部关闭,则应关闭所有单元格;如果用户关闭所有显示时点击视图较少,则应仅关闭选定的单元格。

有什么建议吗?

enter image description here

解决方法

我会使用 IndexSet 来跟踪展开的行;使用集合更容易插入、删除和检查索引路径的存在。

要折叠所有行,您可以简单地从集合中删除所有元素。要展开所有内容,请将所有索引路径插入集合中。

您尚未提供有关如何存储表数据的详细信息。出于回答的目的,我将使用名为 data 的数组数组 - 外部数组是部分,每个内部数组是该部分中的行。即 numberOfSectionsdata.count 且节中的行数为 data[indexPath.section].count

视图控制器


var expanded = [IndexSet]()

//Custom protocol


func loadData() {
    // After data is fetched
    self.expanded = Array(repeating: IndexSet(),count:data.count)
    self.tableView.reloadData()
}

func viewMoreTapped(cell: tableviewcell) {
        
   guard let indexPath = entriesTableView.indexPath(for: cell) else {
       return
   }

   let row = indexPath.row
   let section = indexPath.section

   if self.expanded[section].contains(row) {
       self.expanded[section].remove(row) 
   } else {
       self.expanded[section].insert(row)
   }

   self.entriesTableView.beginUpdates()
   self.entriesTableView.reloadRows(at: [indexPath],with: .none)
   self.entriesTableView.endUpdates()
}

@IBAction func expandAllBtnAction(_ sender: Any) {
    
    isExpandedAll.toggle()
    expandAllButton.titlelabel.text = isExpandedAll ? "Open All" : "Close All"
    
    if isExpandedAll {
        self.expanded = data.map { return IndexSet(integersIn: 0..<$0.count)}
    } else {
        self.expanded = Array(repeating: IndexSet(),count:data.count)
    }
    self.entriesTableView.reloadData()
}
      
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier:"cellIdentifier",for:
         indexPath) as! MyTableViewCell
    cell.cellConfigure(isExpanded: self.expanded[indexPath.section].contains(indexPath.row),info: info) // A cell should never need to know its index path

    return cell
}

细胞

var isExpanded: Bool = false

func cellConfigure(isExpanded : Bool,info: Info) {

    viewMoreBtn.titleLabel?.text = isExpanded ? "View less":"View more"
    cellHeight.constant = isExpanded ? 40 : 120

}

@IBAction func viewMoreBtnAction(_ sender: Any) {
    delegate?.viewMoreTapped(cell: self) // this is custom protocol
}

一般注意事项,变量和函数应以小写字母开头。类和结构名称应以大写字母开头。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...