ios GDataXML(DOM)解析分區(qū)表格

GDataXML是Google開發(fā)的一個(gè)XML解析庫(kù),輕便,特點(diǎn)使用非常簡(jiǎn)單,支持XPath。

前言:GDataXML是google基于C語(yǔ)言寫的第三方框架,該源碼文件就一個(gè)類,看其源碼,基本使用了C語(yǔ)言的底層的很多l(xiāng)ib編譯庫(kù)代碼,所以剛導(dǎo)入使用,會(huì)報(bào)錯(cuò)提示需要設(shè)置導(dǎo)入需要的鏈接庫(kù)。 另外,該第三方框架并沒有納入Cocoapods,所以通過(guò)pod搜索不到這個(gè)框架。

使用第三方框架有兩種方式:

1:使用手動(dòng)導(dǎo)包? 下載網(wǎng)址? http://xiazai.jb51.net/201602/yuanma/GDataXML(jb51.net).zip

下載完成導(dǎo)入項(xiàng)目編譯會(huì)出現(xiàn)

第一步:

第二步:

第三步:

這樣編譯一下報(bào)錯(cuò)就會(huì)消失了!!!

手寫xml文檔



-----------Hero.h-------------

#import@interface Hero : NSObject

@property(nonnull,strong)NSString *name,*like;

@end

------------ViewController.m---------------

#import "ViewController.h"#import "GDataXMLNode.h"#import "Hero.h"@interface ViewController (){? ? NSMutableDictionary *_dic;? ? UITableView *_table;}@end#define TEST_URL @"http://127.0.0.1/1508ESort.xml"@implementation ViewController- (void)viewDidLoad {? ? [super viewDidLoad];? ? // Do any additional setup after loading the view, typically from a nib.? ? _table = [[UITableView alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];? ? _table.delegate = self;? ? _table.dataSource = self;? ? [self.view addSubview:_table];? ? NSURLSession *session = [NSURLSession sharedSession];? ? NSURLSessionDataTask *task = [session dataTaskWithURL:[NSURL URLWithString:TEST_URL] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {? ? ? ? ? ? ? ? GDataXMLDocument *doc = [[GDataXMLDocument alloc]initWithData:data options:0 error:nil];? ? ? ? GDataXMLElement *rootEle = [doc rootElement];? ? ? ? _dic = [[NSMutableDictionary alloc]init];? ? ? ? for (GDataXMLElement * sortEle in [rootEle elementsForName:@"sort"]) {? ? ? ? ? ? //準(zhǔn)備字典的 key? 英雄類型 ===kind? ? ? ? ? ? //sortEle---NSString *kindStr = [[sortEle attributeForName:@"kind"] stringValue];

NSMutableArray *arr = [[NSMutableArray alloc]init];

for (GDataXMLElement *heroEle in [sortEle elementsForName:@"hort"]) {

Hero *he = [[Hero alloc]init];

he.name = [[[heroEle elementsForName:@"name"] firstObject] stringValue];

he.like = [[[heroEle elementsForName:@"like"] firstObject] stringValue];

[arr addObject:he];

}

//給字典設(shè)置數(shù)值

//以英雄類型為key 以該類型的英雄數(shù)組為值

[_dic setObject:arr forKey:kindStr];

}

dispatch_async(dispatch_get_main_queue(), ^{

[_table reloadData];

});

}];

[task resume];

}

//表格的分區(qū)數(shù)

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

//字典中的key值得數(shù)量就是表格的分區(qū)數(shù)

return _dic.count;

}

//表格每個(gè)分區(qū)中的行數(shù)

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

NSString *key = [_dic.allKeys objectAtIndex:section];

return [[_dic objectForKey:key] count];

}

//創(chuàng)建表格單元格中的內(nèi)容

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

//復(fù)用池機(jī)制

static NSString *cellId = @"cellid";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];

if (!cell) {

//設(shè)置表格的樣式

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId];

}

//獲取對(duì)應(yīng)的key值,從key值中判斷需要獲取的數(shù)據(jù)來(lái)為單元格賦值

NSString *key = [_dic.allKeys objectAtIndex:indexPath.section];

cell.textLabel.text = [[[_dic objectForKey:key] objectAtIndex:indexPath.row] name];

cell.detailTextLabel.text = [[[_dic objectForKey:key] objectAtIndex:indexPath.row] like];

return cell;

}

//表格中每個(gè)分區(qū)的標(biāo)題

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

return [_dic.allKeys objectAtIndex:section];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

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

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

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