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

首先導入第三方GDataXML

手寫xml文件

<?xml version="1.0" encoding="utf-8"?>
<root>
    <sort kind = "ADC">
        <hero>
            <name>賞金獵人</name>
            <like>槍林彈雨</like>
        </hero>
        <hero>
            <name>寒冰射手</name>
            <like>萬箭齊發(fā)</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];
            }
            //給字典設置數(shù)值
            //以英雄類型為key 以該類型的英雄數(shù)組為值
            [_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
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末软族,一起剝皮案震驚了整個濱河市懂盐,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌桥滨,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,627評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件载慈,死亡現(xiàn)場離奇詭異耍贾,居然都是意外死亡,警方通過查閱死者的電腦和手機被环,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,180評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來详幽,“玉大人筛欢,你說我怎么就攤上這事〈狡福” “怎么了版姑?”我有些...
    開封第一講書人閱讀 169,346評論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長迟郎。 經(jīng)常有香客問我剥险,道長,這世上最難降的妖魔是什么宪肖? 我笑而不...
    開封第一講書人閱讀 60,097評論 1 300
  • 正文 為了忘掉前任表制,我火速辦了婚禮,結果婚禮上控乾,老公的妹妹穿的比我還像新娘么介。我一直安慰自己,他們只是感情好蜕衡,可當我...
    茶點故事閱讀 69,100評論 6 398
  • 文/花漫 我一把揭開白布壤短。 她就那樣靜靜地躺著,像睡著了一般慨仿。 火紅的嫁衣襯著肌膚如雪久脯。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,696評論 1 312
  • 那天镰吆,我揣著相機與錄音帘撰,去河邊找鬼。 笑死鼎姊,一個胖子當著我的面吹牛骡和,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播相寇,決...
    沈念sama閱讀 41,165評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼慰于,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了唤衫?” 一聲冷哼從身側響起婆赠,我...
    開封第一講書人閱讀 40,108評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎佳励,沒想到半個月后休里,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,646評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡赃承,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,709評論 3 342
  • 正文 我和宋清朗相戀三年妙黍,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片瞧剖。...
    茶點故事閱讀 40,861評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡拭嫁,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出抓于,到底是詐尸還是另有隱情做粤,我是刑警寧澤,帶...
    沈念sama閱讀 36,527評論 5 351
  • 正文 年R本政府宣布捉撮,位于F島的核電站怕品,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏巾遭。R本人自食惡果不足惜肉康,卻給世界環(huán)境...
    茶點故事閱讀 42,196評論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望灼舍。 院中可真熱鬧迎罗,春花似錦、人聲如沸片仿。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,698評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽砂豌。三九已至厢岂,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間阳距,已是汗流浹背塔粒。 一陣腳步聲響...
    開封第一講書人閱讀 33,804評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留筐摘,地道東北人卒茬。 一個月前我還...
    沈念sama閱讀 49,287評論 3 379
  • 正文 我出身青樓船老,卻偏偏與公主長得像,于是被迫代替她去往敵國和親圃酵。 傳聞我的和親對象是個殘疾皇子柳畔,可洞房花燭夜當晚...
    茶點故事閱讀 45,860評論 2 361

推薦閱讀更多精彩內(nèi)容