1

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)
}
}
BangOperator
  • 4,377
  • 2
  • 24
  • 38
Fatih Lale
  • 51
  • 1
  • 8
  • You can implement `UIScrollViewDelegate` method `scrollViewDidScroll`, and if `contentOffset.y` is larger than some threshold, then refresh table view – mag_zbc Jul 27 '17 at 07:58
  • Does your UIViewControlller contains only a single UITableView and not any other elements? – PGDev Jul 27 '17 at 10:28

2 Answers2

1

If I got you correctly, you want to refresh the table view after all the data has been loaded. If it's the case you need parserDidEndDocument.

func parserDidEndDocument(parser: NSXMLParser){
    table?.reloadData()
}

You can take a look at this and/or this for pull to refresh.

Lawliet
  • 3,438
  • 2
  • 17
  • 28
0

Try this EDIT

var refreshControl: UIRefreshControl!

override func viewDidLoad() {
   super.viewDidLoad()

   refreshControl = UIRefreshControl()
   refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
   refreshControl.addTarget(self, action: "refresh:", forControlEvents: UIControlEvents.ValueChanged)
   tableView.addSubview(refreshControl) // not required when using UITableViewController
}

func refresh(sender:AnyObject) {
   // Code to refresh table view  
      parsingDataFromURL()
      tblView.reloadData()
}
Kamlesh Shingarakhiya
  • 2,757
  • 2
  • 16
  • 34