摘自我在SO 问题Reload tableView section without reloading header section - Swift的回答,如果觉得有帮助,请移步SO给个upvote.
只需要code的话,这一段就够了
var indexPaths = self.tableView.indexPathsForVisibleRows
/* just roload visible rows of specified section
indexPaths = indexPaths.filter({ (indexPath) -> Bool in
return indexPath.section == SECTION_INDEX_NEED_TO_RELOAD
})
*/
self.tableView.reloadRows(at: indexPaths!, with: .none)
Note:
UITableView.reloadData()
will reload all the table. Docs about reloaddatareload all the data that is used to construct the table, including
cells, section headers and footers, index arrays, and so on. For
efficiency, the table view redisplays only those rows that are
visible. It adjusts offsets if the table shrinks as a result of the
reload. The table view’s delegate or data source calls this method
when it wants the table view to completely reload its data.UITableView.reloadSections(_ sections: IndexSet, with animation: UITableViewRowAnimation)
will reload specific section( including section header and section footer). Docs about reloadsectionsUITableView.reloadRows(at indexPaths: [IndexPath], with animation: UITableViewRowAnimation)
will reload the specified rows. Docs about reloadRows
So here reloadSections
will not works here. If your header section data and status not change, call this method will make no diffrence by sight.
Solution: use reloadRows
to load the rows of all the sections or of the specific section. Note: here you can just load the visible rows, others will be loaded when you scroll you view. You also can refer to Get all indexPaths in a section for more solutions
var indexPaths = self.tableView.indexPathsForVisibleRows
/* just roload visible rows of specified section
indexPaths = indexPaths.filter({ (indexPath) -> Bool in
return indexPath.section == SECTION_INDEX_NEED_TO_RELOAD
})
*/
self.tableView.reloadRows(at: indexPaths!, with: .none)