{
NSString *new;
NSMutableDictionary *dic;
NSMutableArray *arr;
Model *mo;
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) {? ? ? ? NSXMLParser *xm=[[NSXMLParser alloc]initWithData:data];? ? ? ? xm.delegate=self;? ? ? ? [xm parse];? ? }];? ? [tas resume];? ? ? ? ? ? tab=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];? ? tab.dataSource=self;? ? tab.delegate=self;? ? [self.view addSubview:tab];? ? }//開始- (void)parserDidStartDocument:(NSXMLParser *)parser;{? ? //初始化字典? ? dic=[[NSMutableDictionary alloc]init];? ? ? ? }//結(jié)束- (void)parserDidEndDocument:(NSXMLParser *)parser;{? ? //刷新表格? ? ? ? [tab reloadData];? ? }- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName attributes:(NSDictionary*)attributeDict;
{
if ([elementName isEqualToString:@"a"])
{
//準(zhǔn)備Key==英雄種類
NSString*str=[attributeDict objectForKey:@"king"];
//準(zhǔn)備個數(shù)組
arr=[[NSMutableArray alloc]init];
//設(shè)置字典key和? 值
[dic setObject:arr forKey:str];
}
else if ([elementName isEqualToString:@"fen"])
{
mo=[[Model alloc]init];
[arr addObject:mo];
}
new=elementName;
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName;
{
new=nil;
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string;
{
if ([new isEqualToString:@"name"])
{
mo.name=string;
}
else if ([new isEqualToString:@"age"])
{
mo.age=string;
}
}
-(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 *str4=@"cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:str4];
if (cell==nil)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str4];
}
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];
}