引用第三方库AFNetworking
#import "AFNetworking.h"
#import "UIKit+AFNetworking.h"
- (void)downloadData { NSString *urlString = [NSString stringWithFormat:LIMIT_URL,_page,_categoryId]; AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.responseSerializer = [AFHTTPResponseSerializer serializer]; [manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { //JSON解析数据 NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil]; NSArray *apps = dict[@"applications"]; for (NSDictionary *appDict in apps) { AppModel *model = [[AppModel alloc] init]; [model setValuesForKeysWithDictionary:appDict]; model.desc = appDict[@"description"]; [_dataArray addObject:model]; [self.tableView reloadData];//若刷新表格需在block块内 } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { }]; }
通过AFNetworking,使用url从网上加载图片(#import "UIKit+AFNetworking.h")
[cell.iconImageView setImageWithURL:[NSURL URLWithString:model.iconUrl]];