Kri*_*ofe 4 uitableview uiscrollview ios swift indexpath
在对tableview数据进行某种排序后,我需要重新加载不包括标题的部分.这就是说我只想重新加载该部分中的所有行.但是在我搜索一段时间后,我找不到一个简单的方法.
reloadSections(sectionIndex, with: .none) 这里不起作用,因为它会重新加载整个部分,包括页眉,页脚和所有行.
所以我需要reloadRows(at: [IndexPath], with: UITableViewRowAnimation)改用.但是如何获取该部分中所有行的整个indexPath.
在我看来,你不需要在该部分重新加载整个单元格.简单地说,重新加载可见的单元格以及需要重新加载的内部区域.重新加载不可见的单元格是没用的,因为它们在tableView(_:cellForRowAt:)被调用时将被修复.
试试我的下面的代码
var indexPathsNeedToReload = [IndexPath]()
for cell in tableView.visibleCells {
let indexPath: IndexPath = tableView.indexPath(for: cell)!
if indexPath.section == SECTION_INDEX_NEED_TO_RELOAD {
indexPathsNeedToReload.append(indexPath)
}
}
tableView.reloadRows(at: indexPathsNeedToReload, with: .none)
Run Code Online (Sandbox Code Playgroud)
您可以像这样获取用于重新加载的indexPaths\xe2\x80\xa6
\nlet indexPaths = tableView.visibleCells\n .compactMap(tableView.indexPath)\n .filter { $0.section == SECTION }\nRun Code Online (Sandbox Code Playgroud)\n无需重新加载不可见单元格,因为它们将在以下时间更新:cellForRow(at indexPath:)调用时更新
您可以在给定的部分中使用下面的函数get IndexPath array。
func getAllIndexPathsInSection(section : Int) -> [IndexPath] {
let count = tblList.numberOfRows(inSection: section);
return (0..<count).map { IndexPath(row: $0, section: section) }
}
Run Code Online (Sandbox Code Playgroud)
要么
func getAllIndexPathsInSection(section : Int) -> [IndexPath] {
return tblList.visibleCells.map({tblList.indexPath(for: $0)}).filter({($0?.section)! == section}) as! [IndexPath]
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3096 次 |
| 最近记录: |