#import "ViewController.h"
#import “one.h” //创建的模型、里面声明属性
#import "UIImageView+WebCache.h"
//#import "firstTableViewCell.h" // 使用xib的话引入这个创建的头文件
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *TwoTableView;
@property (nonatomic, strong) UIView *OneView;
@property (nonatomic, strong) NSMutableArray *AllDataArray;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 注册cell
// [self.TwoTableView registerNib:[UINib nibWithNibName:@"firstTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"]; //使用自定义的cell要注册
[self.view addSubview:self.OneView];
[self.view addSubview:self.TwoTableView];
self.TwoTableView.delegate = self;
self.TwoTableView.dataSource = self;
[self loadData];
}
#pragma mark 加载数据
- (void)loadData {
self.AllDataArray = [NSMutableArray array];
__block typeof(self) weakSelf = self;
// 准备网址
NSURL *url = [NSURL URLWithString:@"http://app.9nali.com/index/778?page_id=1&device=iPhone&version=1.1.2"];
// 创建请求对象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 设置请求类型
[request setHTTPMethod:@"GET"];
// 连接服务器
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
// 判断数据是否为空 ,如果是的话就直接返回
if (data == nil) {
return ;
}
// 解析
NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSArray *array = dict[@"list"];
for (NSDictionary *item in array) {
one *l = [one new];
[l setValuesForKeysWithDictionary:item];
[weakSelf.AllDataArray addObject:l];
NSLog(@"%@", weakSelf.AllDataArray[0]);
}
[self.TwoTableView reloadData]; // 刷新数据
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark 懒加载
- (UIView *)OneView {
if (_OneView == nil) {
_OneView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 200)];
_OneView.backgroundColor = [UIColor redColor];
}
return _OneView;
}
- (UITableView *)TwoTableView {
if (_TwoTableView == nil) {
_TwoTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 200, 375, 300) style:UITableViewStyleGrouped];
}
return _TwoTableView;
}
#pragma mark 实现tabllView的代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _AllDataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
one *l = _AllDataArray[indexPath.row];
cell.textLabel.text = l.nickname;
// [cell.imageView sd_setImageWithURL:[NSURL URLWithString:l.mediumLogo] placeholderImage:[UIImage imageNamed:@"music.jpg"]];
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:l.mediumLogo] placeholderImage:[UIImage imageNamed:@"music.jpg"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
}];
return cell;
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end