導(dǎo)入GData第三方
#define? TEST_URL @"__________________"
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSURLSession *session = [NSURLSession sharedSession];
NSURL *url = [NSURL URLWithString:TEST_URL];
NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//創(chuàng)建對象 將二進制數(shù)據(jù)緩存到內(nèi)存中
GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:data options:0 error:nil];
//獲得根標(biāo)簽內(nèi)容
GDataXMLElement *rootEle = [doc rootElement];
//獲得根標(biāo)簽里的hero標(biāo)簽
NSArray *heroArr = [rootEle elementsForName:@"hero"];
arr = [[NSMutableArray alloc] init];
//遍歷
for (GDataXMLElement *heroEle in heroArr) {
Hero *he = [[Hero alloc] init];
//////////////////////////////////////////
//獲得name標(biāo)簽的數(shù)組 (即便只有一個標(biāo)簽對 也會放入數(shù)組)
NSArray *nameArr = [heroEle elementsForName:@"name"];
//因為數(shù)組中只有一個數(shù)據(jù) 則直接取第0個下邊的標(biāo)簽即可
//獲取name標(biāo)簽
GDataXMLElement *nameEle = [nameArr objectAtIndex:0];
//? ? ? ? ? ? GDataXMLElement *nameEle = [nameArr firstObject];
//? ? ? ? ? ? GDataXMLElement *nameEle = [nameArr lastObject];
//去掉標(biāo)簽 寒冰
he.name = [nameEle stringValue];
//////////////////////////////////////////////
//////////////////////////////////////////
he.like = [[[heroEle elementsForName:@"like"] firstObject] stringValue];
/////////////////////////////////////////
[arr addObject:he];
}
}];
[task resume];
//初始化一個表格
_table = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
//設(shè)置代理
_table.delegate = self;
_table.dataSource = self;
//加載
[self.view addSubview:_table];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellID = @"fang";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
}
cell.textLabel.text = [arr[indexPath.row] name];
cell.detailTextLabel.text = [arr[indexPath.row] like];
return cell;
}