简体   繁体   English

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

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

I am doing Swift project, there it has view more/view less option for each cell.我正在做 Swift 项目,每个单元格都有查看更多/查看更少选项。 I have did it with following code.我已经用下面的代码做到了。 And it was for for View more/View less options.它用于查看更多/查看更少选项。

//ViewController Class //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 }

It is working fine for each cell View more/View less , But after user taps on open all , it should be open all cells and each cell if user has taps on view less , it should be close selected cell only.每个单元格都可以正常查看更多/查看更少,但是在用户点击全部打开后,它应该打开所有单元格,如果用户点击查看更少,则应该只关闭选定的单元格。 But, it's not working that time.但是,那个时候是行不通的。

The requirement is要求是

  1. if user taps on view more , it should be open selected cell.如果用户点击查看更多,它应该是打开选定的单元格。 If user taps on view less , it should be close selected cell.如果用户点击view less ,它应该是关闭选定的单元格。
  2. Also if user taps on open all , All cells should be expanded, and also same time if user tap on view less for particular cell, that time only selected cell should be close.此外,如果用户点击open all ,则应展开所有单元格,并且同时如果用户点击特定单元格的 view less ,则只有选定的单元格应该关闭。
  3. If user taps on close all, all cells should be close and if user taps on view less while close all showing, only selected cell should be close.如果用户点击全部关闭,则所有单元格都应该关闭,如果用户在关闭所有显示时点击视图较少,则只有选定的单元格应该关闭。

Any suggestions?有什么建议么?

在此处输入图像描述

I would use an IndexSet to track the expanded rows;我会使用IndexSet来跟踪展开的行; It is easier to insert, remove and check for the presence of index paths using a set.使用集合更容易插入、删除和检查索引路径的存在。

To collapse all of the rows you can simply remove all of the elements from the set.要折叠所有行,您只需从集合中删除所有元素。 To expand all you insert all index paths into the set.要扩展所有,您将所有索引路径插入集合中。

You haven't provided detail on how your table data is stored.您尚未提供有关如何存储表数据的详细信息。 For the purposes of my answer I am going to use an array of arrays called data - The outer array is the sections and each inner array are the rows in that section.出于我回答的目的,我将使用一个名为data的 arrays 数组 - 外部数组是部分,每个内部数组是该部分中的行。 ie numberOfSections is data.count and the number of rows in a section is data[indexPath.section].countnumberOfSectionsdata.count ,一个部分的行数是data[indexPath.section].count

View Controller查看 Controller


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
}

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
}

A general note, variables and functions should start with a lower case letter.一般注意事项,变量和函数应以小写字母开头。 Class and struct names should start with an upper case letter. Class 和结构名称应以大写字母开头。

暂无
暂无

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

相关问题 每个单元打开视图 - Each cell open view iOS Swift使用for循环在TableView单元格中添加两个视图 - iOS Swift Add Two View in TableView Cell with for loop 如何在编辑 tableview 单元格中的文本字段时重新加载所有 tableview 单元格 - iOS Swift - How to reload all tableview cells, while editing a textfield in a tableview cell - iOS Swift 通过单击表视图中的单元格 - Swift iOS打开新的视图控制器 - Open new View Controller by clicking Cell in Table View - Swift iOS 当为iOS选择TableView单元格时,视图已更改 - View has changed when a TableView cell is selected for iOS 如何在集合视图单元格Swift中检测所有OTP文本字段是否已填充 - How to detect all OTP textfields are filled or not in Collection view cell Swift 如何处理在第二个表格视图的行中快速点击第一表格视图单元格下面的水龙头? - How to handle tap on row of 2nd tableView which is under the 1st table view cell in swift? 如何避免在TableView iOS Swift中滚动时刷新每个单元格数据? - how to avoid refresh each cell data on scroll in tableview ios swift? 如何在不重叠表视图中的其他单元格的情况下重用表视图单元格 - How to reuse a table view cell without overlapping other cells in tableview ios swift Swift iOS-如何更改已在屏幕上加载的所有可见单元的TableView单元属性,而无需重新加载数据? - Swift iOS -How to change TableView Cell Properties for All Visible Cells Already Loaded On Screen Without Reloading Data?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM