效果:
LoadData.h
#import#import "Video.h"
#import "VideoInfo.h"
@interface LoadData : NSObject{
NSMutableArray *mArr;
NSString *eleName,*name;
Video *vd;
VideoInfo *vi;
}
+ (instancetype)shareLoadData;
- (void)getMessage:(NSString *)urlStr andStr:(NSString *)str;
LoadData.m
#import "LoadData.h"
static LoadData *ld;
@implementation LoadData
+ (instancetype)shareLoadData
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
ld = [[LoadData alloc]init];
});
return ld;
}
- (void)getMessage:(NSString *)urlStr andStr:(NSString *)str{
name = str;
NSXMLParser *parser = [[NSXMLParser alloc]initWithContentsOfURL:[NSURL URLWithString:urlStr]];
parser.delegate = self;
[parser parse];
}
- (void)parserDidStartDocument:(NSXMLParser *)parser{
mArr = [NSMutableArray array];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary*)attributeDict
{
eleName = elementName;
if ([name isEqualToString:@"video"])
{
if ([eleName isEqualToString:@"video"])
{
vd = [[Video alloc]init];
[mArr addObject:vd];
}
}
else if ([name isEqualToString:@"videoInfo"])
{
if ([eleName isEqualToString:@"video"])
{
vi = [[VideoInfo alloc]init];
[mArr addObject:vi];
}
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if ([name isEqualToString:@"video"]) {
if ([eleName isEqualToString:@"name"]) {
vd.name = string;
}else if ([eleName isEqualToString:@"ids"]) {
vd.ids = string;
}
}else if ([name isEqualToString:@"videoInfo"]) {
if ([eleName isEqualToString:@"image"]) {
vi.image = string;
}else if ([eleName isEqualToString:@"sort"]) {
vi.sort = string;
}else if ([eleName isEqualToString:@"times"]) {
vi.times = string;
}else if ([eleName isEqualToString:@"ids"]) {
vi.ids = string;
}
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
eleName = nil;
}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
[[NSNotificationCenter defaultCenter]postNotificationName:name object:mArr];
}
video.h(继承与NSobject)
@property (nonatomic,strong)NSString *name,*ids;
videInfo.h(继承与NSobject)
@property (nonatomic,strong)NSString *image,*ids,*sort,*times;
第一个界面
#import "ViewController.h"
#import "LoadData.h"
#import "InfoViewController.h"
#import "Video.h"
@interface ViewController (){
NSArray *arr;
UITableView *table;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(getMessage:) name:@"video" object:nil];
[[LoadData shareLoadData]getMessage:@"http://127.0.0.1/VideoProgram.xml" andStr:@"video"];
table = [[UITableView alloc]initWithFrame:self.view.bounds];
table.dataSource = self;
table.delegate = self;
[self.view addSubview:table];
}
- (void)getMessage:(NSNotification *)notice
{
arr = [notice.object copy];
[table reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellid"];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cellid"];
}
Video *vd = arr[indexPath.row];
cell.textLabel.text = vd.name;
cell.detailTextLabel.text = vd.ids;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
InfoViewController *ivc = [[InfoViewController alloc]init];
Video *vd = arr[indexPath.row];
ivc.name = vd.name;
ivc.ids = vd.ids;
[self presentViewController:ivc animated:YES completion:nil];
}
第二个界面.h
@property (nonatomic,strong)NSString *name,*ids;
第二个界面.m
#import "InfoViewController.h"
#import "LoadData.h"
#import "VideoInfo.h"
@interface InfoViewController (){
NSMutableArray *mArr;
UITableView *table;
}
@end
@implementation InfoViewController
- (void)viewDidLoad {
[super viewDidLoad];
//
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(getMessage:) name:@"videoInfo" object:nil];
[[LoadData shareLoadData]getMessage:@"http://127.0.0.1/VideoProgrammeInfo.xml" andStr:@"videoInfo"];
table = [[UITableView alloc]initWithFrame:self.view.bounds];
table.dataSource = self;
table.delegate = self;
[self.view addSubview:table];
}
- (void)getMessage:(NSNotification *)notice
{
mArr = [NSMutableArray array];
NSArray *arr = [notice.object copy];
for (VideoInfo *vi in arr) {
if ([vi.ids isEqualToString:self.ids]) {
[mArr addObject:vi];
}
}
[table reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return mArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellid"];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cellid"];
}
VideoInfo *vi = mArr[indexPath.row];
cell.textLabel.text = self.name;
cell.detailTextLabel.text = [NSString stringWithFormat:@"分类:%@,次数:%@",vi.sort,vi.times];
cell.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:vi.image]]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self dismissViewControllerAnimated:YES completion:nil];
}
videoProgram.xml
videoProgrammelnfo.xml