一次快速展开和收缩 UITableView 第一行 [英] Expand and Contract UITableView row one at a time swift

查看:24
本文介绍了一次快速展开和收缩 UITableView 第一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在展开和折叠表格视图单元格.当前情况运行良好,假设 Row-0 被点击,在 index-1 处添加一个新行并显示为展开,当再次点击 Row-0 时,展开的 index-1 被折叠.如果 Row-0 处于展开状态,即使我点击另一行,该行也会展开并同时保持打开状态.这是我想要更改的内容,我想收缩当我点击另一行时打开的任何其他行.即一次应打开一行.请参阅下面的 ref 代码,我尝试使用数组来检测当前扩展了哪一行,这仅在我们按顺序进行时才有效,否则它会开始删除收缩的行.任何帮助将不胜感激.

I am working on expanding and collapsing table view cell. The current situation works well , where assume Row-0 is tapped a new row is added at index-1 and shown as expanded and when again Row-0 is tapped the expanded index-1 is collapsed. If Row-0 is in expanded state and even if I tap on another Row, the row will expand keeping both open at the same time. Here is what I want to change, I want to contract any other row which is open when I tap on another row. i.e. One row should be open at a time. Please see the below code for ref which I tried using an array to detect which row is currently expanded, this works only if we go sequentially else it starts deleting the contracted rows. Any help will be highly appreciated.

谢谢!

 public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
  {        

  if expandCellArray.object(at: indexPath.row) as! String == "contract"
    {
        for i in 0 ..< self.expandCellArray.count
        {
            if expandCellArray.object(at: i) as! String == "expanded"
            {
                self.contractCell(tableView: tableView, index: i)
                self.expandCellArray.replaceObject(at: i, with: "contract")
                print("Updated Expand Cell array",expandCellArray)
            }
        }
        self.expandCell(tableView: tableView, index: indexPath.row)
        self.expandCellArray.replaceObject(at: indexPath.row, with: "expanded")
        print("Updated Expand Cell array",expandCellArray)

    }
    else
    {
        self.contractCell(tableView: tableView, index: self.selectedIndexPath.row)
    }
 }

扩展行的功能

private func expandCell(tableView: UITableView, index: Int)
 {
        if (dataArray[index]?.title) != nil {
            dataArray.insert(nil, at: index + 1)
            myTableView.insertRows(at: [IndexPath(row: index + 1, section: 0)], with: .none)
    }

 }

合同行的功能

private func contractCell(tableView: UITableView, index: Int)
 {
    if (dataArray[index]?.title) != nil {
        dataArray.remove(at: index + 1)
        myTableView.deleteRows(at: [IndexPath(row: index + 1, section: 0 )], with: .none)
    }
 }

推荐答案

添加或删除单元格很棘手,因为您必须处理索引.或者,一个更简单的实现是使用可扩展部分.

Adding or removing cells is tricky because you have to handle the indexes. Alternatively, a simpler implementation of this is to use expandable sections.

您只需简单地用动画显示和隐藏它们,而不是添加和删除它们.如果您只想保持一个部分打开,当用户单击另一部分时,只需折叠打开的部分并在两个部分(用户单击的部分和当前打开的部分)上重新加载部分

Instead of adding and deleting, you just simply showing and hiding them with animation. If you want to keep only one section open, when user click on another section, simply collapse the opened section and do section reload on both sections(The one that user clicked and the one that is currently open)

这篇关于一次快速展开和收缩 UITableView 第一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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