解析

效果:


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:@"分類:%@,次數(shù):%@",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


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容