获取一个节中的所有indexPaths [英] Get all indexPaths in a section

查看:235
本文介绍了获取一个节中的所有indexPaths的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to reload the section excluding the header after I do some sort on my tableview data. That's to say I just want to reload all the rows in the section. But I don't find a easy way to make it after I search for a while.

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

reloadSections(sectionIndex, with: .none) not works here, for it will reload the whole section including the header, footer and all the rows.

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

So I need to use reloadRows(at: [IndexPath], with: UITableViewRowAnimation) instead. But how to get the whole indexPaths for the all the rows in the section.

推荐答案

我认为,您无需在此部分中重新加载整个单元格.简单地,重新加载可见的单元格,并且需要重新加载内部部分.重新加载不可见的单元格是没有用的,因为在调用tableView(_:cellForRowAt:)时它们将被修复.

In my opinion, you don't need reload whole cells in the section. Simply, reload cells which is visible and inside section you need to reload. Reload invisible cells is useless because they will be fixed when tableView(_:cellForRowAt:) is called.

尝试下面的代码

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)

这篇关于获取一个节中的所有indexPaths的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆