首先导入第三方GDataXML
手写xml文件
<?xml version="1.0" encoding="utf-8"?>
<root>
<sort kind = "ADC">
<hero>
<name>赏金猎人</name>
<like>枪林弹雨</like>
</hero>
<hero>
<name>寒冰射手</name>
<like>万箭齐发</like>
</hero>
<hero>
<name>皮城女警</name>
<like>让子弹飞</like>
</hero>
</sort>
<sort kind = "AP">
<hero>
<name>流浪法师</name>
<like>禁锢</like>
</hero>
<hero>
<name>提莫</name>
<like>种蘑菇</like>
</hero>
</sort>
<sort kind = "T">
<hero>
<name>德玛西亚</name>
<like>三爪</like>
</hero>
<hero>
<name>雷霆咆哮</name>
<like>枪林弹雨</like>
</hero>
<hero>
<name>皮夹龙骨</name>
<like>无线</like>
</hero>
</sort>
</root>
Model类
Hero.h
#import <Foundation/Foundation.h>
@interface Hero : NSObject
@property (nonatomic,strong)NSString *name,*like;
@end
控制器类
ViewController.m
#import "ViewController.h"
#import "GDataXMLNode.h"
#import "Hero.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>{
NSMutableDictionary *_dic;
UITableView *_table;
}
@end
#define TEST_URL @"http://127.0.0.1/textSort.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"]) {
//准备字典的 key 英雄类型 ===kind
//sortEle---<sort kind="ADC">
NSString *kindStr = [[sortEle attributeForName:@"kind"] stringValue];
NSMutableArray *arr = [[NSMutableArray alloc]init];
for (GDataXMLElement *heroEle in [sortEle elementsForName:@"hero"]) {
Hero *he = [[Hero alloc]init];
he.name = [[[heroEle elementsForName:@"name"] firstObject] stringValue];
he.like = [[[heroEle elementsForName:@"like"] firstObject] stringValue];
[arr addObject:he];
}
//给字典设置数值
//以英雄类型为key 以该类型的英雄数组为值
[_dic setObject:arr forKey:kindStr];
}
dispatch_async(dispatch_get_main_queue(), ^{
[_table reloadData];
});
}];
[task resume];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return _dic.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSString *key = [_dic.allKeys objectAtIndex:section];
return [[_dic objectForKey:key] count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellId = @"cellid";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId];
}
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;
}
-(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