重新加载集合视图时如何调用函数?
How to call function when collection view is reloaded?
我有一个集合视图,当它被用户重新加载时,我想 运行 做一些事情。这种重新加载是由于用户在视图上向下拖动直到重新加载图标出现而引起的。将此函数放在除用户进行交互以重新加载集合之外的任何其他位置时,这对我来说是一个不可行的解决方案。
下面的解决方法呢。我为集合-、table- 和滚动视图实现了它,以防其他人遇到类似问题:
final class ScrollViewRefreshDemo: UIViewController {
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
let tableView = UITableView()
let scrollView = UIScrollView()
override func viewDidLoad() {
super.viewDidLoad()
attachRefreshListener(to: collectionView)
attachRefreshListener(to: tableView)
attachRefreshListener(to: scrollView)
}
func attachRefreshListener(to scrollView: UIScrollView) {
scrollView.refreshControl?.addTarget(self, action: #selector(scrollViewDidRefresh), for: .valueChanged)
}
@objc
func scrollViewDidRefresh() {
// Execute any task while the scroll view is refreshing
}
}
我有一个集合视图,当它被用户重新加载时,我想 运行 做一些事情。这种重新加载是由于用户在视图上向下拖动直到重新加载图标出现而引起的。将此函数放在除用户进行交互以重新加载集合之外的任何其他位置时,这对我来说是一个不可行的解决方案。
下面的解决方法呢。我为集合-、table- 和滚动视图实现了它,以防其他人遇到类似问题:
final class ScrollViewRefreshDemo: UIViewController {
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
let tableView = UITableView()
let scrollView = UIScrollView()
override func viewDidLoad() {
super.viewDidLoad()
attachRefreshListener(to: collectionView)
attachRefreshListener(to: tableView)
attachRefreshListener(to: scrollView)
}
func attachRefreshListener(to scrollView: UIScrollView) {
scrollView.refreshControl?.addTarget(self, action: #selector(scrollViewDidRefresh), for: .valueChanged)
}
@objc
func scrollViewDidRefresh() {
// Execute any task while the scroll view is refreshing
}
}