如何在iOS Swift中处理所有打开/关闭的TableView单元格中的每个单元格都具有View More/View less选项 [英] How to handle open all/close all in tableview cell with each cell has View More/View less options in iOS Swift

查看:49
本文介绍了如何在iOS Swift中处理所有打开/关闭的TableView单元格中的每个单元格都具有View More/View less选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在执行Swift项目,该项目为每个单元格提供了查看更多/查看更少的选项.我已经用下面的代码做到了.它用于查看更多/查看更少的选项.

I am doing Swift project, there it has view more/view less option for each cell. I have did it with following code. And it was for for View more/View less options.

//ViewController类

//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.

要求是

  1. 如果用户点击查看更多,则应打开所选单元格.如果用户点击少观看,则应关闭所选单元格.
  2. 如果用户点击全部打开,则所有单元格都应展开,同时如果用户点击较少的特定单元格,则此时只有选中的单元格应该关闭.
  3. 如果用户点按全部关闭",则所有单元格都应关闭;如果用户点按视图时少点关闭所有显示",则应仅关闭所选单元格.
  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.
  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.
  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.

有什么建议吗?

推荐答案

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

I would use an IndexSet to track the expanded rows; 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.

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

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. ie numberOfSections is data.count and the number of rows in a section is 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
}

一般说明,变量和函数应以小写字母开头.类和结构名称应以大写字母开头.

A general note, variables and functions should start with a lower case letter. Class and struct names should start with an upper case letter.

这篇关于如何在iOS Swift中处理所有打开/关闭的TableView单元格中的每个单元格都具有View More/View less选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆