XML表格解析

#import “ViewController.h”
#import “AFNetworking/AFNetworking.h”
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{

NSMutableDictionary *_dic;
UITableView *_tbv;

}
@end
#define TEST_URL @“http://127.0.0.1/1608E.json
@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];

    _tbv = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];

    _tbv.delegate = self;
    _tbv.dataSource = self;

    [self.view addSubview:_tbv];

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //设置解析器类型
    manager.responseSerializer = [[AFJSONResponseSerializer alloc]init];

    NSURL *url = [NSURL URLWithString:TEST_URL];

    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];

    NSURLSessionDataTask *task = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {

    self->_dic = responseObject;
    [self->_tbv reloadData];
    }];

    [task resume];
    }
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return _dic.count;
    }
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    NSString *str1 = [_dic.allKeys objectAtIndex:section];

    return [[_dic objectForKey:str1]count];

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *str = @“cell”;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];

if(!cell){

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:str];
} NSString *str1 = [_dic.allKeys objectAtIndex:indexPath.section];
NSString *str2 = [[[_dic objectForKey:str1]objectAtIndex:indexPath.row]objectForKey:@“img”];
cell.textLabel.text = [[[_dic objectForKey:str]objectAtIndex:indexPath.row]objectForKey:@“img”];

NSURL *imgUrl = [NSURL URLWithString:str2];
NSData *imgData = [NSData dataWithContentsOfURL:imgUrl];

cell.imageView.image = [[UIImage alloc]initWithData:imgData];

cell.textLabel.text = [[[_dic objectForKey:str1]objectAtIndex:indexPath.row]objectForKey:@“name”];

cell.textLabel.text = [[[_dic objectForKey:str1]objectAtIndex:indexPath.row]objectForKey:@“like”];

return cell;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值