下記のソースコードを元にRSSリーダーを作成しています。
更新ボタンを設置し、ボタンを押下した際に再度パースを行い、TableViewを再描画したいのですが、どのように書けば良いでしょうか。
以下、簡易のソースコードになります。よろしくお願い致します。
class ListViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, NSXMLParserDelegate { // outlet - table view @IBOutlet var myTableView: UITableView! // outet - activity indicator @IBOutlet var spinner: UIActivityIndicatorView! // xml parser var myParser: NSXMLParser = NSXMLParser() // rss records var rssRecordList : [RssRecord] = [RssRecord]() var rssRecord : RssRecord? var isTagFound = [ "item": false , "title":false, "pubDate": false ,"link":false] // MARK - View functions override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. // set tableview delegate self.myTableView.dataSource = self self.myTableView.delegate = self } override func viewDidAppear(animated: Bool) { // load Rss data and parse if self.rssRecordList.isEmpty { self.loadRSSData() } } // MARK: - Table view dataSource and Delegate // return cell func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { // collect reusable cell let cell = tableView.dequeueReusableCellWithIdentifier("rssCell", forIndexPath: indexPath) // find record for current cell let thisRecord : RssRecord = self.rssRecordList[indexPath.row] // set value for main title and detail tect cell.textLabel?.text = thisRecord.title cell.detailTextLabel?.text = thisRecord.pubDate // return cell return cell } @IBAction func refreshButtonClicked(sender: UIBarButtonItem) { //更新ボタンを押したとき self.rssRecordList = [RssRecord]() self.loadRSSData() self.myTableView.reloadData() } // MARK: - NSXML Parse delegate function // start parsing document func parserDidStartDocument(parser: NSXMLParser) { // start parsing } // element start detected func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) { if elementName == "item" { self.isTagFound["item"] = true self.rssRecord = RssRecord() }else if elementName == "title" { self.isTagFound["title"] = true }else if elementName == "link" { self.isTagFound["link"] = true }else if elementName == "pubDate" { self.isTagFound["pubDate"] = true } } // characters received for some element func parser(parser: NSXMLParser, foundCharacters string: String) { if isTagFound["title"] == true { self.rssRecord?.title += string }else if isTagFound["link"] == true { self.rssRecord?.link += string }else if isTagFound["pubDate"] == true { self.rssRecord?.pubDate += string } } // element end detected func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) { if elementName == "item" { self.isTagFound["item"] = false self.rssRecordList.append(self.rssRecord!) }else if elementName == "title" { self.isTagFound["title"] = false }else if elementName == "link" { self.isTagFound["link"] = false }else if elementName == "pubDate" { self.isTagFound["pubDate"] = false } } // end parsing document func parserDidEndDocument(parser: NSXMLParser) { //reload table view self.myTableView.reloadData() // stop spinner self.spinner.stopAnimating() } // if any error detected while parsing. func parser(parser: NSXMLParser, parseErrorOccurred parseError: NSError) { // stop animation self.spinner.stopAnimating() } // MARK: - Utility functions // load rss and parse it private func loadRSSData(){ if let rssURL = NSURL(string: RSS_FEED_URL) { // start spinner self.spinner.startAnimating() // fetch rss content from url self.myParser = NSXMLParser(contentsOfURL: rssURL)! // set parser delegate self.myParser.delegate = self self.myParser.shouldResolveExternalEntities = false // start parsing self.myParser.parse() } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/12/16 06:49