【问题标题】:How to reload selected cell in particular section for expand/collapse in tableview Swift如何在tableview Swift中重新加载特定部分中的选定单元格以展开/折叠
【发布时间】:2021-05-12 02:36:04
【问题描述】:

我正在我的 iOS 应用程序中进行展开/折叠表格视图单元格功能。我有多个部分。每个部分都有多个单元格。默认情况下,单元格高度为 100,一旦用户点击单元格,我将高度增加到 200。

所以,基于 Bool 值,我正在更改它。但是,在滚动表格视图时,它正在交换部分之间的展开/折叠单元格。 就像我点击第一部分的第一个单元格一样,它正在扩展,但在滚动 tableview 之后,第二部分的第一个单元格也在扩展。

我的要求是,如果用户点击特定单元格,则该单元格仅应展开/折叠。用户可以手动展开和关闭。用户可以展开多个单元格。

所以,我尝试存储 Indexpath 行和部分。

         var expandedIndexSet : IndexSet = []
             var expandedIndexSection : IndexSet = []
         
             func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                 let cell = tableView.dequeueReusableCell(withIdentifier:"cellIdentifier", for:
         indexPath) as! MyTableViewCell
         
if expandedIndexSet.contains(indexPath.row) && expandedIndexSection.contains(indexPath.section) { // expanded true
                     cell.height = 200
                    //some other data loading here
                 }
                 else {  //expanded false
              cell.height = 100
                 }
              }
         
             @IBAction moreButtonTapped(_ sender: Any) {
                 
                 if(expandedIndexSet.contains(indexPath.row)) && expandedIndexSection.contains(indexPath.section){
                     expandedIndexSet.remove(indexPath.row)
                     expandedIndexSection.remove(indexPath.section)
                     
                 } else {
                     expandedIndexSet.insert(indexPath.row)
                     expandedIndexSection.insert(indexPath.section)
                 }
                 entriesTableView.beginUpdates()
                 entriesTableView.reloadRows(at: [indexPath], with: .none)
                 entriesTableView.endUpdates()
             }

谁能给出比这更好的方法?

【问题讨论】:

    标签: ios swift iphone uitableview expandablelistview


    【解决方案1】:

    如果您将部分和行独立存储在单独的数组中,您的算法将失败。 原因是两者都是依赖的: 想想三个展开的单元格(行:1,部分:1),(行:2,部分:1),(行:3,部分:2)

    现在单元格(行:3,部分:1)会发生什么? row-array 包含值“3”,section-array 包含值“1”,因此将被视为展开。

    因此,需要将索引路径作为一个整体存储——见示例代码:

    var expanded:[IndexPath] = []
    
    expanded.append(IndexPath(row:1, section:1))
    expanded.append(IndexPath(row:2, section:1))
    expanded.append(IndexPath(row:3, section:2))
    
    let checkPath = IndexPath(row:3, section:1)
    if (expanded.contains(checkPath)) {
        print ("is expanded")
    } else {
        print ("collapsed")
    }
    

    更新

    因此,在您的按钮句柄中,您将执行以下操作:

    @IBAction moreButtonTapped(_ sender: Any) {
        
        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()
    }
    

    【讨论】:

    • 感谢@AndreasOetjen 的宝贵建议,但是在按钮操作中如何插入和删除扩展的索引路径?
    • 检查我更新的答案 - 我不知道您如何获得与按钮对应的索引路径,但您似乎以某种方式存储它。
    • 谢谢,我正在从按钮标签中获取索引路径,此代码运行良好,它比我的代码更好。谢谢@AndreasOetjen 我接受了你的回答并投票了
    • 对此stackoverflow.com/questions/66420575/…的任何建议我必须显示全部打开/全部关闭
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多