[英]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要求是
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].count
即numberOfSections
是data.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.