【发布时间】: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