如何处理 open all/close all in tableview cell with each cell has View More/View less options in iOS Swift

How to handle open all/close all in tableview cell with each cell has View More/View less options in iOS Swift

我正在做 Swift 项目,其中每个单元格都有 view more/view less 选项。我已经用下面的代码做到了。这是为了查看 more/View 更少的选项。

//ViewController Class

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, info: Info, expandedAll: isExpandedAll)
        }

    return cell

}

//Tableview cell class

    var isExpanded: Bool = false

    func cellConfigure(index: Int, isexpanded : Bool, info: Info, 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
    }

每个单元格都工作正常View more/View less,但在用户点击 open all 后,它应该如果用户点击 view less,则打开所有单元格和每个单元格,它应该只关闭选定的单元格。但是,那个时候没用。

要求是

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

有什么建议吗?

我会使用 IndexSet 来跟踪展开的行;使用集合可以更轻松地插入、删除和检查索引路径是否存在。

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

您没有详细说明您的 table 数据是如何存储的。出于我的回答目的,我将使用一个名为 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..<[=10=].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
}

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