Oc GDataXML(DOM)解析分区表格

首先导入第三方GDataXML

手写xml文件



    
        
            赏金猎人
            枪林弹雨
        
        
            寒冰射手
            万箭齐发
        
        
            皮城女警
            让子弹飞
        
    


    
        流浪法师
        禁锢
    
    
        提莫
        种蘑菇
    




    
        德玛西亚
        三爪
    
    
        雷霆咆哮
        枪林弹雨
    
    
        皮夹龙骨
        无线
    






Model类
Hero.h

#import 

@interface Hero : NSObject
@property (nonatomic,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/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---
            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

你可能感兴趣的:(Oc GDataXML(DOM)解析分区表格)