我正在使用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调用

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

谢谢。


当前回答

斯威夫特4

var refreshControl: UIRefreshControl!

override func viewDidLoad() {
    super.viewDidLoad()

    refreshControl = UIRefreshControl()
    refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
    refreshControl.addTarget(self, action: #selector(refresh), for: .valueChanged)
    tableView.addSubview(refreshControl) 
}

@objc func refresh(_ sender: Any) {
    //  your code to reload tableView
}

你可以停止刷新:

refreshControl.endRefreshing()

其他回答

您可以通过使用几行代码来实现这一点。所以为什么你会困在第三方库或UI中。 拉动刷新是iOS内置的。你可以像这样快速完成

var pullControl = UIRefreshControl()

override func viewDidLoad() {
   super.viewDidLoad()

   pullControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
   pullControl.addTarget(self, action: #selector(pulledRefreshControl(_:)), for: UIControl.Event.valueChanged)
   tableView.addSubview(pullControl) // not required when using UITableViewController
}

@objc func pulledRefreshControl(sender:AnyObject) {
   // Code to refresh table view  
}

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

拉动刷新是iOS内置的。你可以像这样快速完成

let refreshControl = UIRefreshControl()

override func viewDidLoad() {
   super.viewDidLoad()

   refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
   refreshControl.addTarget(self, action: #selector(self.refresh(_:)), for: .valueChanged)
   tableView.addSubview(refreshControl) // not required when using UITableViewController
}

@objc func refresh(_ sender: AnyObject) {
   // Code to refresh table view  
}

在某些时候,你可能会停止刷新。

refreshControl.endRefreshing()

Anhil的回答对我帮助很大。

然而,在进一步试验之后,我注意到建议的解决方案有时会导致不那么漂亮的UI故障。

相反,采用这种方法*让我成功了。

*迅速2.1

//Create an instance of a UITableViewController. This will host your UITableView.
private let tableViewController = UITableViewController()

//Add tableViewController as a childViewController and set its tableView property to your UITableView.
self.addChildViewController(self.tableViewController)
self.tableViewController.tableView = self.tableView
self.refreshControl.addTarget(self, action: "refreshData:", forControlEvents: .ValueChanged)
self.tableViewController.refreshControl = self.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()来完成刷新