IOS reload rows of secitons or a specific section

6 篇文章 0 订阅

摘自我在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 reloaddata

    reload 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 reloadsections

  • UITableView.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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值