获取部分中的所有indexPath

Kri*_*ofe 4 uitableview uiscrollview ios swift indexpath

在对tableview数据进行某种排序后,我需要重新加载不包括标题的部分.这就是说我只想重新加载该部分中的所有行.但是在我搜索一段时间后,我找不到一个简单的方法.

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

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

tru*_*duc 7

在我看来,你不需要在该部分重新加载整个单元格.简单地说,重新加载可见的单元格以及需要重新加载的内部区域.重新加载不可见的单元格是没用的,因为它们在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)

  • 是的,我看到了!但是能做什么呢.可能他认为他可以获得一些回购.但我同意关于可见细胞的最初想法是你的.所以只有+1. (2认同)

Ash*_*lls 7

您可以像这样获取用于重新加载的indexPaths\xe2\x80\xa6

\n
let indexPaths = tableView.visibleCells\n    .compactMap(tableView.indexPath)\n    .filter { $0.section == SECTION }\n
Run Code Online (Sandbox Code Playgroud)\n

无需重新加载不可见单元格,因为它们将在以下时间更新:cellForRow(at indexPath:)调用时更新

\n


PPL*_*PPL 5

您可以在给定的部分中使用下面的函数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)

  • '0 .. &lt;count'是编写它的首选方式 (2认同)