异步解析xml读新浪新闻作业总结

前端之家收集整理的这篇文章主要介绍了异步解析xml读新浪新闻作业总结前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

首先,single view

在storyboard里,拉出来一个navigationbar 然后把后面的tableview换成原来的那个viewcontroller

把viewcontroller里去掉原先继承的类

再拉一个tabbar进来

拉一个button在viewcontroller上面

然后连接button和tabbar

恩,就是这个样子


接着把tabbar后面全都换成tableviewcontroller(拉出来4个tableviewcontroller)

把所有的cell改成subtitle后连接一个viewcontroller

改一下segue

拉个webview在最后那个viewcontroller上

然后就可以写代码拉。。

首先写最后那个viewcontroller的类

把webview连接出来

新建两个属性,一个是获得weburl,一个是获得webtitle

  1. //
  2. // webNewsViewController.h
  3. // fixedOne
  4. //
  5. // Created by ioscourse on 13-4-16.
  6. // Copyright (c) 2013年 cct. All rights reserved.
  7. //
  8.  
  9. #import <UIKit/UIKit.h>
  10.  
  11. @interface webNewsViewController : UIViewController
  12.  
  13. @property (weak,nonatomic) IBOutlet UIWebView *webView;
  14. @property (strong,nonatomic) NSString *webUrl;
  15. @property (strong,nonatomic) NSString *webTitle;
  16.  
  17. @end

然后看 .m
  1. #import "webNewsViewController.h"
  2.  
  3. @interface webNewsViewController ()
  4.  
  5. @end
  6.  
  7. @implementation webNewsViewController
  8.  
  9. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  10. {
  11. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  12. if (self) {
  13. // Custom initialization
  14. }
  15. return self;
  16. }
  17.  
  18. - (void)viewDidLoad
  19. {
  20. [super viewDidLoad];
  21. // Do any additional setup after loading the view.
  22. // scale
  23. self.webView.scalesPageToFit = YES;
  24. // title
  25. self.navigationItem.title = self.title;
  26. // load html
  27. [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.webUrl]]];
  28. }
  29.  
  30. - (void)didReceiveMemoryWarning
  31. {
  32. [super didReceiveMemoryWarning];
  33. // Dispose of any resources that can be recreated.
  34. }
  35.  
  36. @end

然后在.pch里加上后面要用的xml

  1. #define focusNewsUrlOfXml @"http://RSS.sina.com.cn/news/marquee/ddt.xml"
  2. #define internationalNewsUrlOfXml @"http://RSS.sina.com.cn/news/world/focus15.xml"
  3. #define domesticNewsUrlOfXml @"http://RSS.sina.com.cn/news/china/focus15.xml"
  4. #define socialNewsUrlOfXml @"http://RSS.sina.com.cn/news/society/focus15.xml"


这个是对于tableview的抽象类的.h

  1. @property (strong,nonatomic) NSString *cellName;
  2. @property (strong,nonatomic) NSString *urlOfXml;
  3. @property (strong,nonatomic) NSMutableArray *pubdate;
  4. @property (strong,nonatomic) NSMutableArray *link;
  5. @property (strong,nonatomic) NSMutableArray *tit;
  6. @property (strong,nonatomic) NSMutableString *stringTemp;
  7.  
  8. - (void)initWithTableViewName;

噢,那个,记得加载NSXMLParserDelegate和webviewcontroller的那个.h啊

然后viewdidload函数

使用异步来做这个

  1. [self initWithTableViewName];
  2. dispatch_queue_t que = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0);
  3. dispatch_async(que,^{
  4. NSURL *url = [NSURL URLWithString:self.urlOfXml];
  5. NSXMLParser *parser = [[NSXMLParser alloc]initWithContentsOfURL:url];
  6. [parser setDelegate:self];
  7. [parser parse];
  8. // 清空备用string
  9. self.stringTemp = nil;
  10. dispatch_async(dispatch_get_main_queue(),^{
  11. [self.tableView reloadData];
  12. });
  13. });

异步使用的具体情况,等过段时间我再写一篇

这个就是最开始segue改名的时候把原来的默认的叫item的那个标题改成新闻要闻之类的东西


  1. - (void)initWithTableViewName
  2. {
  3. if ([self.title isEqualToString:@"新闻要闻"]) {
  4. self.urlOfXml = focusNewsUrlOfXml;
  5. self.cellName = @"focusNewsCell";
  6. }
  7. else if ([self.title isEqualToString:@"国际新闻"]) {
  8. self.urlOfXml = internationalNewsUrlOfXml;
  9. self.cellName = @"internationalNewsCell";
  10. }
  11. else if ([self.title isEqualToString:@"国内新闻"]) {
  12. self.urlOfXml = domesticNewsUrlOfXml;
  13. self.cellName = @"domesticNewsCell";
  14. }
  15. else if ([self.title isEqualToString:@"社会新闻"]) {
  16. self.urlOfXml = socialNewsUrlOfXml;
  17. self.cellName = @"socialNewsCell";
  18. }
  19. }


然后解析xml

  1. - (void) parser:(NSXMLParser *)parser
  2. didStartElement:(NSString *)elementName
  3. namespaceURI:(NSString *)namespaceURI
  4. qualifiedName:(NSString *)qName
  5. attributes:(NSDictionary *)attributeDict
  6. {
  7. if ([elementName isEqualToString:@"title"]) {
  8. if (self.tit == nil) {
  9. self.tit = [[NSMutableArray alloc] init];
  10. }
  11. }
  12. if ([elementName isEqualToString:@"link"]) {
  13. if (self.link == nil) {
  14. self.link = [[NSMutableArray alloc] init];
  15. }
  16. }
  17. else if ([elementName isEqualToString:@"pubDate"]){
  18. if (self.pubdate == nil) {
  19. self.pubdate = [[NSMutableArray alloc] init];
  20. }
  21. }
  22. }
  23.  
  24. - (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
  25. if (self.stringTemp == nil)
  26. self.stringTemp = [[NSMutableString alloc] init];
  27. [self.stringTemp appendString:string];
  28. }
  29.  
  30. - (void) parser:(NSXMLParser *)parser
  31. didEndElement:(NSString *)elementName
  32. namespaceURI:(NSString *)namespaceURI
  33. qualifiedName:(NSString *)qName
  34. {
  35. if ([elementName isEqualToString:@"title"]) {
  36. [self.tit addObject:self.stringTemp];
  37. }
  38. else if ([elementName isEqualToString:@"link"]) {
  39. [self.link addObject:self.stringTemp];
  40. }
  41. else if ([elementName isEqualToString:@"pubDate"]) {
  42. [self.pubdate addObject:self.stringTemp];
  43. }
  44. self.stringTemp = nil;
  45. }

获得标签为title的两个之间的内容存在stringtmp里面

然后当找到第二个title的时候把这个字符串赋值给对应的属性


然后设定行数为日起

  1. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  2. {
  3. // Return the number of rows in the section.
  4. return [self.pubdate count] - 2;
  5. }

设定为出版日期数组的个数减去2

为什么减去2

可以看一下xml源码

可以发现第一个和最后一个是不大对的,所以要去掉

  1. cell.textLabel.text = self.tit[indexPath.row+2];
  2. cell.detailTextLabel.text = self.pubdate[indexPath.row+1];


设定cell,title的前两个不要,出版日期第一个不要,最后一个不要

准备

  1. NSMutableString *str = [[NSMutableString alloc] init];
  2. [str appendString:[self.link objectAtIndex:self.tableView.indexPathForSelectedRow.row+2]];
  3. [str deleteCharactersInRange:NSMakeRange(0,4)];
  4. [segue.destinationViewController setWebUrl:str];
  5. [segue.destinationViewController setWebTitle: segue.identifier];


然后link前有空格要删掉一些才可以用

恩,就这样啦,给个demo

猜你在找的XML相关文章