ios 在 ViewController 中的 UICollectionView 中拉动刷新

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35362440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 08:28:59  来源:igfitidea点击:

Pull to refresh in UICollectionView in ViewController

iosswift

提问by David Seek

I used the following code at a UICollectionViewController

我在一个 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 UICollectionViewinside a UIViewControllerand I googled for an hour now but can't get it working. I appreciate any help.

现在我需要它与UICollectionView内部一起工作,UIViewController我现在用谷歌搜索了一个小时,但无法让它工作。我很感激任何帮助。

采纳答案by fishspy

From the storyboard, you need to link the collection view to the appropriate controller file, using Ctrl + Drag from the collection view object. It needs to be linked via an @IBOutlet.

在情节提要中,您需要使用 Ctrl + 从集合视图对象中拖动将集合视图链接到适当的控制器文件。它需要通过@IBOutlet 链接。

Also, you should Ctrl + Drag from the collection view to the view controller object on the storyboard and select Data Source and Delegate so that the collection view is correctly linked.

此外,您应该 Ctrl + 从集合视图拖动到故事板上的视图控制器对象,然后选择数据源和委托,以便正确链接集合视图。

Let me know if this helps or if I have misunderstood your situation.

让我知道这是否有帮助,或者我是否误解了您的情况。

回答by Nagarjun

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

在调用操作方法中更改了新的 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() {
   self.collectionView!.refreshControl.beginRefreshing()
   //code to execute during refresher
       .
       .
       .
   stopRefresher()         //Call this to stop refresher
 }

func stopRefresher() {
   self.collectionView!.refreshControl.endRefreshing()
 }

回答by Subin K Kuriakose

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 
  }

回答by Alessandro Francucci

Swift 5 solution

斯威夫特 5 解决方案

As @fishspy already mentioned, that's the way to put your collection view inside a view controller, but I'm gonna share also how to connect your refresh control to the collection view in a cleaner way.

正如@fishspy 已经提到的,这是将集合视图放入视图控制器的方法,但我还将分享如何以更简洁的方式将刷新控件连接到集合视图。

Since iOS 10 there's a dedicated property for the refresh control. Apart of that, I'd also recommend to directly initialise your refresh control as a property, declaring it private and doing the following things:

从 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()
}

回答by Haseeb Javed

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()
 }