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

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

首先,single view@H_403_1@

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

把viewcontroller里去掉原先继承的类@H_403_1@

再拉一个tabbar进来@H_403_1@

拉一个button在viewcontroller上面@H_403_1@

然后连接button和tabbar@H_403_1@

恩,就是这个样子@H_403_1@


@H_403_1@

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

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

改一下segue@H_403_1@

拉个webview在最后那个viewcontroller上@H_403_1@

然后就可以写代码拉。。@H_403_1@

首先写最后那个viewcontroller的类@H_403_1@

把webview连接出来@H_403_1@

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

//
//  webNewsViewController.h
//  fixedOne
//
//  Created by ioscourse on 13-4-16.
//  Copyright (c) 2013年 cct. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface webNewsViewController : UIViewController

@property (weak,nonatomic) IBOutlet UIWebView *webView;
@property (strong,nonatomic) NSString *webUrl;
@property (strong,nonatomic) NSString *webTitle;

@end

然后看 .m
#import "webNewsViewController.h"

@interface webNewsViewController ()

@end

@implementation webNewsViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    // scale
    self.webView.scalesPageToFit = YES;
    // title
    self.navigationItem.title = self.title;
    // load html
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.webUrl]]];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

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

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


@H_403_1@

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

@property (strong,nonatomic) NSString *cellName;
@property (strong,nonatomic) NSString *urlOfXml;
@property (strong,nonatomic) NSMutableArray *pubdate;
@property (strong,nonatomic) NSMutableArray *link;
@property (strong,nonatomic) NSMutableArray *tit;
@property (strong,nonatomic) NSMutableString *stringTemp;

- (void)initWithTableViewName;

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

然后viewdidload函数:@H_403_1@

使用异步来做这个@H_403_1@

[self initWithTableViewName];
    dispatch_queue_t que = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0);
    dispatch_async(que,^{
        NSURL *url = [NSURL URLWithString:self.urlOfXml];
        NSXMLParser *parser = [[NSXMLParser alloc]initWithContentsOfURL:url];
        [parser setDelegate:self];
        [parser parse];
        
        // 清空备用string
        self.stringTemp = nil;
        dispatch_async(dispatch_get_main_queue(),^{
            [self.tableView reloadData];
        });
    });

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

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


@H_403_1@

- (void)initWithTableViewName
{
    if ([self.title isEqualToString:@"新闻要闻"]) {
        self.urlOfXml = focusNewsUrlOfXml;
        self.cellName = @"focusNewsCell";
    }
    else if ([self.title isEqualToString:@"国际新闻"]) {
        self.urlOfXml = internationalNewsUrlOfXml;
        self.cellName = @"internationalNewsCell";
    }
    else if ([self.title isEqualToString:@"国内新闻"]) {
        self.urlOfXml = domesticNewsUrlOfXml;
        self.cellName = @"domesticNewsCell";
    }
    else if ([self.title isEqualToString:@"社会新闻"]) {
        self.urlOfXml = socialNewsUrlOfXml;
        self.cellName = @"socialNewsCell";
    }
}


然后解析xml@H_403_1@

- (void) parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
   namespaceURI:(NSString *)namespaceURI
  qualifiedName:(NSString *)qName
     attributes:(NSDictionary *)attributeDict
{
    
    if ([elementName isEqualToString:@"title"]) {
        if (self.tit == nil) {
            self.tit = [[NSMutableArray alloc] init];
        }
    }
    if ([elementName isEqualToString:@"link"]) {
        if (self.link == nil) {
            self.link = [[NSMutableArray alloc] init];
        }
    }
    else if ([elementName isEqualToString:@"pubDate"]){
        if (self.pubdate == nil) {
            self.pubdate = [[NSMutableArray alloc] init];
        }
    }
}

- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    if (self.stringTemp == nil)
        self.stringTemp = [[NSMutableString alloc] init];
    [self.stringTemp appendString:string];
}

- (void) parser:(NSXMLParser *)parser
  didEndElement:(NSString *)elementName
   namespaceURI:(NSString *)namespaceURI
  qualifiedName:(NSString *)qName
{
    if ([elementName isEqualToString:@"title"]) {
        [self.tit addObject:self.stringTemp];
    }
    else if ([elementName isEqualToString:@"link"]) {
        [self.link addObject:self.stringTemp];
    }
    else if ([elementName isEqualToString:@"pubDate"]) {
        [self.pubdate addObject:self.stringTemp];
    }
    self.stringTemp = nil;
}

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

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


@H_403_1@

然后设定行数为日起@H_403_1@

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

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

为什么减去2@H_403_1@

可以看一下xml源码@H_403_1@

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

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


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

准备@H_403_1@

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


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

恩,就这样啦,给个demo@H_403_1@

猜你在找的XML相关文章