快速重新加载数据 UICollectionView

标签 swift

var refreshControl: UIRefreshControl = UIRefreshControl()

override func viewDidLoad() {
    super.viewDidLoad()
    collectionview.delegate = self
    collectionview.dataSource = self
    refreshControl.addTarget(self, action: #selector(HomeViewController.refreshData), for: UIControlEvents.valueChanged)
    fetchPosts()
    collectionview.addSubview(refreshControl)

}

@objc func refreshData() {
    refreshControl.endRefreshing()

}

我有一个带有一些 ViewController 的 UITabBar。在 ViewController 中,我有一个 UICollectionView,其中有用户的图像。每个用户都可以加载图像,并且可以通过 UICollectionView 在 Controller 中看到该图像。一旦加载照片,它就不会出现在 UICollectionView 中,因为必须刷新数据。在 RefreshData 函数中我应该做什么来刷新 UICollectionView?

最佳答案

您可以使用以下方法刷新 Collection View :

@objc func refreshData() {
    collectionview.reloadData()
    refreshControl.endRefreshing()
}

您还可以考虑使用简单的图像缓存库,如AlamofireImage例如,避免为此刷新完整的 Collection View 。

关于快速重新加载数据 UICollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50524795/

相关文章:

ios - 如何使用 swift 水平添加 tableview 将 UICollectionView 实现到 UITableviewCell 中?

ios - didSet vs willSet?

ios - SKAction 持续时间如何与帧速率相关?

ios - 具有图像背景和模糊效果的 TableView - swift

ios - Swift 屏蔽 UIView 约束问题

ios - 间歇性问题设置 UISwitch

ios - 在 Swift 中将闭包设置为变量并尝试从 Objective C 类调用

swift - coreML 验证输入失败

ios - 具有多个自定义单元格类型的 RxSwift TableView

swift - 为什么 swift 中要在这个参数前面加#