我在UICollectionViewController
override func viewDidLoad() {
self.collectionView!.alwaysBounceVertical = true
let refresher = UIRefreshControl()
refresher.addTarget(self, action: "refreshStream", forControlEvents: .ValueChanged)
refreshControl = refresher
collectionView!.addSubview(refreshControl!)
}
func refreshStream() {
print("refresh")
self.collectionView?.reloadData()
refreshControl?.endRefreshing()
}
现在我需要它在UICollectionView
内使用UIViewController
并且我用Google搜索了一个小时,但无法使其正常工作。
我感谢任何帮助。
答案 0 :(得分:51)
在调用操作方法时更改了新的swift代码,你可以像这样重写
@IBOutlet weak var collectionView: UICollectionView!
var refresher:UIRefreshControl!
override func viewDidLoad() {
super.viewDidLoad()
self.refresher = UIRefreshControl()
self.collectionView!.alwaysBounceVertical = true
self.refresher.tintColor = UIColor.red
self.refresher.addTarget(self, action: #selector(loadData), for: .valueChanged)
self.collectionView!.addSubview(refresher)
}
func loadData() {
//code to execute during refresher
.
.
.
stopRefresher() //Call this to stop refresher
}
func stopRefresher() {
self.refresher.endRefreshing()
}
答案 1 :(得分:17)
var refreshControl:UIRefreshControl!
override func viewDidLoad() {
super.viewDidLoad()
self.refreshControl = UIRefreshControl()
self.refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
self.refreshControl.addTarget(self, action: #selector(PricingPlansCollectionViewController.reload), for: .valueChanged)
collectionView!.addSubview(refreshControl)
}
func refresh(sender:AnyObject)
{
//DO
}
答案 2 :(得分:11)
在故事板中,您需要使用Ctrl + Drag从集合视图对象将集合视图链接到相应的控制器文件。它需要通过@IBOutlet链接。
此外,您应该从集合视图中按Ctrl + Drag拖动到情节提要板上的视图控制器对象,然后选择数据源和委托,以便正确链接集合视图。
让我知道这是否有帮助,或者我是否误解了你的情况。
答案 3 :(得分:1)
对我来说,我有:
func setupCollectionView(){
self.refreshControl = UIRefreshControl()
self.refreshControl.attributedTitle = NSAttributedString(string: "Refreshing content...")
self.refreshControl.addTarget(self, action: #selector(self.reload), for: .valueChanged)
collectionView!.addSubview(refreshControl)
}
它仍然无法正常工作(我相信我以某种方式阻止了它),所以我补充说:
collectionView.alwaysBounceVertical = true
然后一切都很好。以防万一其他人也面临同样的问题。
为了更好的理解,完整的实现
@IBOutlet private weak var collectionView: UICollectionView!
let refreshControl = UIRefreshControl()
override func viewDidLoad() {
super.viewDidLoad()
setupCollectionView()
}
func setupCollectionView(){
collectionView.delegate = self
collectionView.dataSource = self
collectionView.alwaysBounceVertical = true
self.refreshControl = UIRefreshControl()
self.refreshControl.attributedTitle = NSAttributedString(string: "Refreshing content...")
self.refreshControl.addTarget(self, action: #selector(self.refresh), for: .valueChanged)
collectionView!.addSubview(refreshControl)
}
@objc func refresh(_ sender:AnyObject) {
//do refreshing stuff
}
答案 4 :(得分:0)
正如@fishspy已经提到的那样,这是将集合视图放入视图控制器中的方法,但是我还将分享如何以更简洁的方式将刷新控件连接到集合视图的方法。
自iOS 10起,刷新控件具有专用属性。除此之外,我还建议直接将刷新控件初始化为一个属性,将其声明为私有并执行以下操作:
@IBOutlet private weak var collectionView: UICollectionView!
private let refreshControl = UIRefreshControl()
override func viewDidLoad() {
super.viewDidLoad()
refreshControl.addTarget(self, action: #selector(didPullToRefresh(_:)), for: .valueChanged)
collectionView.alwaysBounceVertical = true
collectionView.refreshControl = refreshControl // iOS 10+
}
@objc
private func didPullToRefresh(_ sender: Any) {
// Do you your api calls in here, and then asynchronously remember to stop the
// refreshing when you've got a result (either positive or negative)
refreshControl.endRefreshing()
}
答案 5 :(得分:0)
private let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(youFunction), for: .valueChanged)
refreshControl.tintColor = UIColor.red
refreshControl.attributedTitle = NSAttributedString(string: "Fetchin Data ...", attributes: nil)
DispatchQueue.main.async {
self.refreshControl.endRefreshing()
yourCollectionViewOrTableView.reloadData()
}