在 tableView 上拉刷新

Posted

技术标签:

【中文标题】在 tableView 上拉刷新【英文标题】:Pull to Refresh on tableView 【发布时间】:2017-07-19 12:10:07 【问题描述】:

我想拉刷新我的 RSS 提要新闻应用程序。我使用导航控制器和表格视图控制器。在 Table View Controller 上我应该怎么做?我们应该添加什么?我的代码在这里。

class TableviewController: UITableViewController 

var items : Array<Item> = []
var entries : Array<Entry> = []

override func viewDidLoad() 
    super.viewDidLoad()


    loadRSS()
    loadAtom()


override func didReceiveMemoryWarning() 
    super.didReceiveMemoryWarning()



override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    return items.count


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
    let cell = tableView.dequeueReusableCell(withIdentifier: "ItemCell", for: indexPath)

    let item:Item = self.items[indexPath.row]
    cell.textLabel?.text = item.title
    cell.detailTextLabel?.text = self.pubDateStringFromDate(item.pubDate! as Date)

    return cell


override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 

    let item = self.items[indexPath.row]

    let url:URL = URL(string: item.link!)!
    let safariViewController = SFSafariViewController(url: url, entersReaderIfAvailable: true)
    present(safariViewController, animated: true, completion: nil)


func loadRSS() 

    let feedUrlString:String = "websiteurl"

    Alamofire.request(feedUrlString).response  response in

        if let data = response.data, let _ = String(data: data, encoding: .utf8) 

            TIFeedParser.parseRSS(xmlData: data as NSData, completionHandler: (isSuccess, channel, error) -> Void in

                if (isSuccess) 
                    self.items = channel!.items!
                    self.tableView.reloadData()
                

                if (response.error != nil) 
                    print((response.error?.localizedDescription)! as String)
                
            )
        
    



func loadAtom() 

    let feedUrlString:String = "websiteurl"

    Alamofire.request(feedUrlString).response  response in

        if let data = response.data, let _ = String(data: data, encoding: .utf8) 

            TIFeedParser.parseAtom(xmlData: data as NSData, completionHandler: (isSuccess, feed, error) -> Void in

                if (isSuccess) 
                    self.entries = feed!.entries!
                    self.tableView.reloadData()
                

                if (error != nil) 
                    print((error?.localizedDescription)! as String)
                
            )
        
    


func pubDateStringFromDate(_ pubDate:Date)->String 
    let format = DateFormatter()
    format.dateFormat = "yyyy/M/d HH:mm"

    let pubDateString = format.string(from: pubDate)
    return pubDateString


【问题讨论】:

【参考方案1】:

1) 将UIRefreshControl 类型的属性声明为:

 var refreshControl = UIRefreshControl()

2) 在viewDidLoad()method 中,将此 refreshControl 添加到您的表格视图中:

   self.refreshControl.addTarget(self, action: #selector(loadRSS), for: .valueChanged)

    if #available(ios 10, *)
        self.tableView.refreshControl = self.refreshControl
    else
       self.tableView.addSubview(self.refreshControl)
    

【讨论】:

【参考方案2】:

在 TableViewController 中声明一个属性 refreshControl 为

   var refreshControl:UIRefreshControl?

viewDidLoad()函数中添加如下代码

 refreshControl?.addTarget(self, action: #selector(loadRSS), for: .valueChanged)
 self.tableView.addSubview(refreshControl!)

将您的 loadRSS() 函数更新为

func loadRSS() 

    let feedUrlString:String = "websiteurl"

    Alamofire.request(feedUrlString).response  response in
 self.refreshControl?.endRefreshing(
        if let data = response.data, let _ = String(data: data, encoding: .utf8) 

            TIFeedParser.parseRSS(xmlData: data as NSData, completionHandler: (isSuccess, channel, error) -> Void in

                if (isSuccess) 
                    self.items = channel!.items!
                    self.tableView.reloadData()
                

                if (response.error != nil) 
                    print((response.error?.localizedDescription)! as String)
                
            )
        
    

 

【讨论】:

【参考方案3】:

请参考以下代码,因为我在您的代码上添加了拉动刷新控制。

class TableviewController: UITableViewController 

    var items : Array<Item> = []
    var entries : Array<Entry> = []

    var  pullToFrefreshNews:UIRefreshControl!

    override func viewDidLoad() 
        super.viewDidLoad()

        loadRSS()
        loadAtom()

        self.addPulltoRefrehs()
    

    func addPulltoRefrehs() 
        self.pullToFrefreshNews = UIRefreshControl()
        self.pullToFrefreshNews.addTarget(self, action: #selector(TableviewController.loadRSS), for: UIControlEvents.valueChanged)
        self.newsTableVU.addSubview(pullToFrefreshNews)
    


    override func didReceiveMemoryWarning() 
        super.didReceiveMemoryWarning()
    


    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
        return items.count
    

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
        let cell = tableView.dequeueReusableCell(withIdentifier: "ItemCell", for: indexPath)

        let item:Item = self.items[indexPath.row]
        cell.textLabel?.text = item.title
        cell.detailTextLabel?.text = self.pubDateStringFromDate(item.pubDate! as Date)

        return cell
    

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 

        let item = self.items[indexPath.row]

        let url:URL = URL(string: item.link!)!
        let safariViewController = SFSafariViewController(url: url, entersReaderIfAvailable: true)
        present(safariViewController, animated: true, completion: nil)
    

    func loadRSS() 

        let feedUrlString:String = "websiteurl"

        Alamofire.request(feedUrlString).response  response in

            if let data = response.data, let _ = String(data: data, encoding: .utf8) 

                TIFeedParser.parseRSS(xmlData: data as NSData, completionHandler: (isSuccess, channel, error) -> Void in

                    if (isSuccess) 
                        self.items = channel!.items!
                        self.tableView.reloadData()
                    

                    if (response.error != nil) 
                        print((response.error?.localizedDescription)! as String)
                    
                )
            
        

    

    func loadAtom() 

        let feedUrlString:String = "websiteurl"

        Alamofire.request(feedUrlString).response  response in

            if let data = response.data, let _ = String(data: data, encoding: .utf8) 

                TIFeedParser.parseAtom(xmlData: data as NSData, completionHandler: (isSuccess, feed, error) -> Void in

                    if (isSuccess) 
                        self.entries = feed!.entries!
                        self.tableView.reloadData()
                    

                    if (error != nil) 
                        print((error?.localizedDescription)! as String)
                    
                )
            
        
    

    func pubDateStringFromDate(_ pubDate:Date)->String 
        let format = DateFormatter()
        format.dateFormat = "yyyy/M/d HH:mm"

        let pubDateString = format.string(from: pubDate)
        return pubDateString
    

【讨论】:

以上是关于在 tableView 上拉刷新的主要内容,如果未能解决你的问题,请参考以下文章

如何在表格视图底部显示上拉刷新控件?

关于MJRefresh 上拉刷新无法停止的解决方法

[BS-23] AFN网络请求上拉/下拉刷新的细节问题总结

iOS-上拉刷新下拉加载 新版MJRefresh和EGOTableViewPullRefresh

iOS常用刷新控件(下拉、上拉)详解

vue loadMore 上拉刷新不能实现的坑