美文网首页
GDataXMLElement解析多分区

GDataXMLElement解析多分区

作者: 法库德 | 来源:发表于2017-09-19 08:27 被阅读0次

    #import "GDataXMLNode.h"

    {

    NSMutableDictionary *dic;

    UITableView *tab;

    }

    # define MAC @"http://127.0.0.1/LOL.xml"

    {

    [super viewDidLoad];

    //

    NSURL *url=[NSURL URLWithString:MAC];

    NSURLSession * see=[NSURLSession sharedSession];

    NSURLSessionDataTask *tas=[see dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

    //创建对象

    GDataXMLDocument *the=[[GDataXMLDocument alloc]initWithData:data options:0 error:nil];

    //获取跟标签

    GDataXMLElement *rootel=[the rootElement];

    //初始化字典

    dic=[[NSMutableDictionary alloc]init];

    for (GDataXMLElement *sor in [rootel elementsForName:@"a"])

    {

    NSString *key=[[sor attributeForName:@"king"]stringValue];

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

    //给数组添加数据

    for (GDataXMLElement *hfenelem in [sor elementsForName:@"fen"])

    {

    Model *mo=[[Model alloc]init];

    mo.name=[[[hfenelem elementsForName:@"name"]firstObject]stringValue];

    mo.age=[[[hfenelem elementsForName:@"age"]firstObject]stringValue];

    [arr addObject:mo];

    }

    //给字典赋值

    [dic setObject:arr forKey:key];

    }

    //回到主线程刷新表格

    dispatch_async(dispatch_get_main_queue(), ^{

    //刷新表格

    [tab reloadData];

    });

    }];

    [tas resume];

    tab=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];

    tab.delegate=self;

    tab.dataSource=self;

    [self.view addSubview:tab];

    }

    -(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 *str=@"cell";

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:str];

    if (cell==nil)

    {

    cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str];

    }

    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]age];

    return cell;

    }

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

    {

    return [dic.allKeys objectAtIndex:section];

    }

    相关文章

      网友评论

          本文标题: GDataXMLElement解析多分区

          本文链接:https://www.haomeiwen.com/subject/qnoisxtx.html