Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pull to refresh in UICollectionView in ViewController

Tags:

ios

swift

I used the following code at a 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()  } 

Now I need it to work with a UICollectionView inside a UIViewController and I googled for an hour now but can't get it working. I appreciate any help.

like image 348
David Seek Avatar asked Feb 12 '16 12:02

David Seek


People also ask

How do I refresh UICollectionView?

Since iOS 10, the UITableView and UICollectionView classes have a refreshControl property. You can add a refresh control to a table or collection view by assigning an instance of the UIRefreshControl class to this property.


2 Answers

New swift code changed in calling action method you could do rewrite like this

@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() {    self.collectionView!.refreshControl.beginRefreshing()    //code to execute during refresher        .        .        .    stopRefresher()         //Call this to stop refresher  }  func stopRefresher() {    self.collectionView!.refreshControl.endRefreshing()  } 
like image 99
Nagarjun Avatar answered Sep 17 '22 00:09

Nagarjun


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    } 
like image 43
Subin K Kuriakose Avatar answered Sep 18 '22 00:09

Subin K Kuriakose