【问题标题】:Expand and Contract UITableView row one at a time swift快速展开和收缩 UITableView 一行
【发布时间】:2017-06-18 00:39:59
【问题描述】:

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

谢谢!

 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)
    }
 }

【问题讨论】:

    标签: ios swift uitableview


    【解决方案1】:

    添加或删除单元格很棘手,因为您必须处理索引。或者,更简单的实现是使用expandable sections

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

    【讨论】:

      猜你喜欢
      • 2019-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-09
      • 1970-01-01
      相关资源
      最近更新 更多