首先,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];
准备
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
原文链接:https://www.f2er.com/xml/300588.html