首页 > 解决方案 > 获取一个section中的所有indexPaths

问题描述

在对表格视图数据进行某种排序后,我需要重新加载不包括标题的部分。也就是说我只想重新加载该部分中的所有行。但是经过一段时间的搜索,我没有找到一种简单的方法。

reloadSections(sectionIndex, with: .none)在这里不起作用,因为它将重新加载整个部分,包括页眉、页脚和所有行。

所以我需要reloadRows(at: [IndexPath], with: UITableViewRowAnimation)改用。但是如何获取该部分中所有行的整个 indexPaths。

标签: iosswiftuitableviewuiscrollviewindexpath

解决方案


您可以在给定部分中使用以下函数获取 IndexPath 数组。

func getAllIndexPathsInSection(section : Int) -> [IndexPath] {
    let count = tblList.numberOfRows(inSection: section);        
    return (0..<count).map { IndexPath(row: $0, section: section) }
}

或者

func getAllIndexPathsInSection(section : Int) -> [IndexPath] {
    return tblList.visibleCells.map({tblList.indexPath(for: $0)}).filter({($0?.section)! == section}) as! [IndexPath]
}

推荐阅读