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/