异步解析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

//
//  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

    #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"


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

@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函数:

使用异步来做这个

[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的那个标题改成新闻要闻之类的东西


- (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

- (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的时候把这个字符串赋值给对应的属性


然后设定行数为日起

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

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

为什么减去2

可以看一下xml源码

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

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


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

准备

    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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念
xml文件介绍及使用
xml编程(一)-xml语法
XML文件结构和基本语法
第2章 包装类
XML入门的常见问题(二)
Java对象的强、软、弱和虚引用
JS解析XML文件和XML字符串详解
java中枚举的详细使用介绍
了解Xml格式
XML入门的常见问题(四)
深入SQLite多线程的使用总结详解
PlayFramework完整实现一个APP(一)
XML和YAML的使用方法
XML轻松学习总节篇