所以,我有这个加载rss feed的应用程序,我已经实现了刷新功能。上传刷新,它会重新加载并添加新帖子,如果它已发布,但之前的数据保留在那里并被添加到堆栈中。检查代码和图像。我在下面附上。
//
// TableViewController.swift
// WebView
//
import UIKit
class TableViewController: UITableViewController,XMLParserDelegate{
var currentElement:String = ""
var postTitle:String = ""
var webLink:String = ""
var feeds:[Model] = []
var reload: UIRefreshControl! = UIRefreshControl()
override func viewDidLoad() {
super.viewDidLoad()
//let urlToSend: NSURL = NSURL(string: "https://sensodyneforsensitiveteeth.wordpress.com/feed")!
let urlToSend: NSURL = NSURL(string: "http://froshgeek.com/feed")!
let parser = XMLParser(contentsOf: urlToSend as URL)!
parser.delegate = self
parser.parse()
//reload
reload!.attributedTitle = NSAttributedString(string: "Pull to reload the page")
reload!.addTarget(self, action: #selector(TableViewController.reloadFunc), for: UIControlEvents.valueChanged)
tableView.addSubview(reload)
reload.endRefreshing()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
currentElement=elementName;
if(elementName=="item")
{
postTitle = String()
webLink = String()
}
}
func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
if(elementName=="item"){
let feed: Model = Model()
feed.postTitle = postTitle
feed.webLink = webLink
feeds.append(feed)
}
}
func parser(_ parser: XMLParser, foundCharacters string: String) {
let data = string.replacingOccurrences(of: "^\\s*", with: "", options: .regularExpression)
if (!data.isEmpty){
if currentElement == "title" {
postTitle += data
}
else if currentElement == "link" {
webLink += data
}
}
}
func parser(_ parser: XMLParser, parseErrorOccurred parseError: Error) {
print("Bad XML File, Please validate")
}
func reloadFunc(_ sender: AnyObject){
viewDidLoad()
self.tableView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return feeds.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "webFeeds", for: indexPath)
let feeds: Model = self.feeds[indexPath.row]
cell.textLabel?.text = feeds.postTitle
return cell
}
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "webParse" {
let feed: Model = feeds[tableView.indexPathForSelectedRow!.row]
let pageView = segue.destination as! ViewController
pageView.webLink = feed.webLink
}
}
func parserDidEndDocument(_ parser: XMLParser){
self.tableView.reloadData();
self.refreshControl?.endRefreshing();
}
}
Model.swift
//
// Model.swift
// WebView
//
import Foundation
class Model {
var postTitle:String = ""
var webLink:String = ""
var currentElement:String = ""
}
ViewController.swift
//
// ViewController.swift
// WebView
//
import UIKit
class ViewController: UIViewController, UIWebViewDelegate {
@IBOutlet weak var showPage: UIWebView!
var webLink: String = String()
override func viewDidLoad() {
super.viewDidLoad()
let link : NSURL = NSURL(string: webLink)!
let req : NSURLRequest = NSURLRequest(url: link as URL)
showPage.loadRequest (req as URLRequest)
showPage.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
重新加载时的屏幕截图