我想为我的RSS Feed新闻应用程序刷新。我使用View控制器和UITableView
。我应该怎么做View Controller?我们应该添加什么?我的代码在这里。
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)
}
}
答案 0 :(得分:1)
如果我找到了你,你想在加载所有数据后刷新表格视图。如果是这种情况,则需要parserDidEndDocument
。
func parserDidEndDocument(parser: NSXMLParser){
table?.reloadData()
}
答案 1 :(得分:0)
试试这个 的修改
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()
}