在tableView上拉到刷新

时间:2017-07-19 12:10:08

标签: ios swift xcode pull-to-refresh

我想为我的RSS Feed新闻应用程序刷新。我使用导航控制器和表视图控制器。我应该如何在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
}
}

3 个答案:

答案 0 :(得分:2)

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

 var refreshControl = UIRefreshControl()

2)在viewDidLoad()方法中,将此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)
    }

答案 1 :(得分:1)

在TableViewController中声明属性refreshControl为

   var refreshControl:UIRefreshControl?

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

} 

答案 2 :(得分:1)

请参考以下代码,因为我已经为您的代码添加了pull to refresh control。

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