我正在使用swift构建一个RSS阅读器,需要实现重新加载功能。

以下是我如何努力做到这一点。

class FirstViewController: UIViewController,
    UITableViewDelegate, UITableViewDataSource {

   @IBOutlet var refresh: UIScreenEdgePanGestureRecognizer
   @IBOutlet var newsCollect: UITableView

   var activityIndicator:UIActivityIndicatorView? = nil

   override func viewDidLoad() {
       super.viewDidLoad()
       self.newsCollect.scrollEnabled = true
      // Do any additional setup after loading the view, typically from a nib.

      if nCollect.news.count <= 2{
          self.collectNews()
       }
      else{
          self.removeActivityIndicator()
       }
      view.addGestureRecognizer(refresh)
   }



@IBAction func reload(sender: UIScreenEdgePanGestureRecognizer) {
    nCollect.news = News[]()
    return newsCollect.reloadData()
}

我得到:

财产的自我。刷新'未在super初始化。init调用

请帮助我理解手势识别器的行为。一个工作样例代码将是一个很大的帮助。

谢谢。


当前回答

我构建了一个RSS提要应用程序,在其中我有一个Pull To refresh功能,它最初有上面列出的一些问题。

但是为了补充上面用户的答案,我到处寻找我的用例,但没有找到它。我正在从网络(RSSFeed)下载数据,我想下拉我的故事tableView来刷新。

上面提到的内容涵盖了正确的领域,但人们遇到了一些问题,以下是我所做的,它很有效:

我采取了@Blankarsch的方法,去了我的主。然后选择表视图来使用刷新,然后没有提到的是创建IBOutlet和IBAction来有效地使用刷新

//Created from main.storyboard cntrl+drag refresh from left scene to assistant editor @IBOutlet weak var refreshButton: UIRefreshControl override func viewDidLoad() { ...... ...... //Include your code ...... ...... //Is the function called below, make sure to put this in your viewDidLoad //method or not data will be visible when running the app getFeedData() } //Function the gets my data/parse my data from the web (if you havnt already put this in a similar function) //remembering it returns nothing, hence return type is "-> Void" func getFeedData() -> Void{ ..... ..... } //From main.storyboard cntrl+drag to assistant editor and this time create an action instead of outlet and //make sure arguments are set to none and note sender @IBAction func refresh() { //getting our data by calling the function which gets our data/parse our data getFeedData() //note: refreshControl doesnt need to be declared it is already initailized. Got to love xcode refreshControl?.endRefreshing() }

希望这能帮助到和我处境相同的人

其他回答

我构建了一个RSS提要应用程序,在其中我有一个Pull To refresh功能,它最初有上面列出的一些问题。

但是为了补充上面用户的答案,我到处寻找我的用例,但没有找到它。我正在从网络(RSSFeed)下载数据,我想下拉我的故事tableView来刷新。

上面提到的内容涵盖了正确的领域,但人们遇到了一些问题,以下是我所做的,它很有效:

我采取了@Blankarsch的方法,去了我的主。然后选择表视图来使用刷新,然后没有提到的是创建IBOutlet和IBAction来有效地使用刷新

//Created from main.storyboard cntrl+drag refresh from left scene to assistant editor @IBOutlet weak var refreshButton: UIRefreshControl override func viewDidLoad() { ...... ...... //Include your code ...... ...... //Is the function called below, make sure to put this in your viewDidLoad //method or not data will be visible when running the app getFeedData() } //Function the gets my data/parse my data from the web (if you havnt already put this in a similar function) //remembering it returns nothing, hence return type is "-> Void" func getFeedData() -> Void{ ..... ..... } //From main.storyboard cntrl+drag to assistant editor and this time create an action instead of outlet and //make sure arguments are set to none and note sender @IBAction func refresh() { //getting our data by calling the function which gets our data/parse our data getFeedData() //note: refreshControl doesnt need to be declared it is already initailized. Got to love xcode refreshControl?.endRefreshing() }

希望这能帮助到和我处境相同的人

斯威夫特5

private var pullControl = UIRefreshControl()

pullControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
        pullControl.addTarget(self, action: #selector(refreshListData(_:)), for: .valueChanged)
        if #available(iOS 10.0, *) {
            tableView.refreshControl = pullControl
        } else {
            tableView.addSubview(pullControl)
        }
// Actions
@objc private func refreshListData(_ sender: Any) {
        self.pullControl.endRefreshing() // You can stop after API Call
        // Call API
    }

UIRefreshControl直接支持在每个UICollectionView, UITableView和UIScrollView(需要iOS 10+)!

这些视图中的每一个都有一个refreshControl实例属性,这意味着不再需要在你的滚动视图中添加它作为子视图,你所要做的就是:

@IBOutlet weak var collectionView: UICollectionView!

override func viewDidLoad() {
    super.viewDidLoad()
    
    let refreshControl = UIRefreshControl()
    refreshControl.addTarget(self, action: #selector(doSomething), for: .valueChanged)
    
    // this is the replacement of implementing: "collectionView.addSubview(refreshControl)"
    collectionView.refreshControl = refreshControl
}

@objc func doSomething(refreshControl: UIRefreshControl) {
    print("Hello World!")
    
    // somewhere in your code you might need to call:
    refreshControl.endRefreshing()
}

就我个人而言,我觉得将它作为滚动视图的属性比将它作为子视图添加更自然,特别是因为唯一合适的视图作为UIRefreshControl的父视图是一个滚动视图,即使用UIRefreshControl的功能只在与滚动视图一起工作时有用;这就是为什么这种方法应该更明显地设置刷新控件视图。

但是,你仍然可以选择基于iOS版本使用addSubview:

if #available(iOS 10.0, *) {
  collectionView.refreshControl = refreshControl
} else {
  collectionView.addSubview(refreshControl)
}

你可以使用tableView的子类:

import UIKit

protocol PullToRefreshTableViewDelegate : class {
    func tableViewDidStartRefreshing(tableView: PullToRefreshTableView)
}

class PullToRefreshTableView: UITableView {

    @IBOutlet weak var pullToRefreshDelegate: AnyObject?
    private var refreshControl: UIRefreshControl!
    private var isFirstLoad = true

    override func willMoveToSuperview(newSuperview: UIView?) {
        super.willMoveToSuperview(newSuperview)

        if (isFirstLoad) {
            addRefreshControl()
            isFirstLoad = false
        }
    }

    private func addRefreshControl() {
        refreshControl = UIRefreshControl()
        refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
        refreshControl.addTarget(self, action: "refresh", forControlEvents: .ValueChanged)
        self.addSubview(refreshControl)
    }

    @objc private func refresh() {
       (pullToRefreshDelegate as? PullToRefreshTableViewDelegate)?.tableViewDidStartRefreshing(self)
    }

    func endRefreshing() {
        refreshControl.endRefreshing()
    }

}

1 -在接口构建器改变类你的tableView PullToRefreshTableView或创建一个PullToRefreshTableView编程

在你的视图控制器中实现PullToRefreshTableViewDelegate

当表视图开始刷新时,tableViewDidStartRefreshing(tableView: PullToRefreshTableView)将在视图控制器中被调用

4 -调用yourTableView.endRefreshing()来完成刷新

使用storyboard和Swift的解决方案:

Open your .storyboard file, select a TableViewController in your storyboard and "Enable" the Table View Controller: Refreshing feature in the Utilities. Open the associated UITableViewController class and add the following Swift 5 line into the viewDidLoad method. self.refreshControl?.addTarget(self, action: #selector(refresh), for: UIControl.Event.valueChanged) Add the following method above the viewDidLoad method func refresh(sender:AnyObject) { // Updating your data here... self.tableView.reloadData() self.refreshControl?.endRefreshing() }