I want to pull to refresh for my RSS feed news app. I using View controller and UITableView
. How should I do on View Controller? What should we add? My code is here.
import UIKit
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate,XMLParserDelegate {
@IBOutlet weak var tblView: UITableView!
var parser = XMLParser()
var news = NSMutableArray()
var elements = NSMutableDictionary()
var element = NSString()
var title1 = NSMutableString()
var link = NSMutableString()
var desc = NSMutableString()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
parsingDataFromURL()
addNavBarImage()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func parsingDataFromURL()
{
news = []
parser = XMLParser(contentsOf: URL(string: "feedurl")!)!
parser.delegate = self
parser.parse()
tblView.reloadData()
}
func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
element = elementName as NSString
if(elementName as NSString) .isEqual(to: "item")
{
elements = NSMutableDictionary()
elements = [:]
title1 = NSMutableString()
title1 = " "
link = NSMutableString()
link = " "
}
}
func parser(_ parser: XMLParser, foundCharacters string: String) {
if element .isEqual(to: "title")
{
title1.append(string)
}
else if element.isEqual(to: "description")
{
desc.append(string)
}
else if element.isEqual(to: "link")
{
link.append(string)
}
}
func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
if (elementName as NSString) .isEqual(to: "item")
{
if !title1 .isEqual(nil)
{
elements.setObject(title1, forKey: "title" as NSCopying)
}
if !link .isEqual(nil)
{
elements.setObject(link, forKey: "link" as NSCopying)
}
if !desc.isEqual(nil)
{
elements.setObject(desc, forKey: "description" as NSCopying)
}
news.add(elements)
}
}
internal func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return news.count
}
internal func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
var cell = tableView.dequeueReusableCell(withIdentifier: "myCell")! as UITableViewCell
if (cell.isEqual(NSNull.self))
{
cell = Bundle.main.loadNibNamed("myCell", owner: self, options: nil)?[0] as! UITableViewCell
}
cell.textLabel?.text = (news.object(at: indexPath.row) as AnyObject).value(forKey: "title") as! NSString as String
cell.detailTextLabel?.text = (news.object(at: indexPath.row) as AnyObject).value(forKey: "link") as! NSString as String
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
tableView.deselectRow(at: indexPath, animated: true)
let newsdetailVC = storyboard?.instantiateViewController(withIdentifier: "NewsDetailViewController") as! NewsDetailViewController
newsdetailVC.selectedTitle = (news.object(at: indexPath.row) as AnyObject).value(forKey: "title") as! NSString as String
newsdetailVC.selectedUrl = (news.object(at: indexPath.row) as AnyObject).value(forKey: "link") as! NSString as String
self.navigationController?.pushViewController(newsdetailVC, animated: true)
}
}