#import "ViewController.h"
@interface ViewController ()
{
NSMutableArray *_listarr; //定义一个存放数据的容器。
}
@end
@implementation ViewController
- ( void )viewDidLoad
{
[super viewDidLoad];
//ios7新特性,这个请自行百度。
[self.tableview registerClass:[UITableViewCell class ] forCellReuseIdentifier:@ "Cell" ];
NSError *error;
//加载一个NSURL对象
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@ "http://m.touzhu.cn/ex_jc.aspx?state=0&ifgetclass=1" ]];
//将请求的url数据放到NSData对象中
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//IOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
//获取数组list的内容
NSArray *list = [dic objectForKey:@ "list" ];
NSLog(@ "list数组的内容为--》%@" , list );
//初始化成员变量
_listarr = [[NSMutableArray alloc]init];
//遍历数组list里的内容
for ( int i = 0; i<[list count]; i++) {
//按数组中的索引取出对应的字典
NSDictionary *listdic = [list objectAtIndex:i];
//通过字典中的key取出对应value,并且强制转化为NSString类型
NSString *teamname = (NSString *)[listdic objectForKey:@ "name_j" ];
//将获取的value值放到数组容器中
[_listarr addObject:teamname];
NSLog(@ "name内容为--》%@" , teamname );
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//必须返回与数据容器一样的数量,否则会报数据越界错
return [_listarr count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @ "Cell" ;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
//将要显示的数据赋值到cell上
cell.textLabel.text = [_listarr objectAtIndex:indexPath.row];
return cell;
}
@end
|