{
// NSMutableArray *arr;
}
@property(nonatomic,strong)UICollectionViewLayout *CollectionLayout;
@property(nonatomic,strong)UICollectionView *CollectionView;
@property(nonatomic,strong)NSMutableArray *MutableArray;
@end
@implementation recommendViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.title=@"推荐";
//CollectionView
[self collection];
//网络请求
[self request];
}
-(void)request {
NSString *urlStr = @"http://live.9158.com/Fans/GetHotLive?page=%ld";
NSString *str1 = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
self.MutableArray=[NSMutableArray array];
[Request url:str1 parameter:nil record:^(NSDictionary *record) {
// NSLog(@"%@",result);
NSDictionary *Data=record[@"data"];
// NSLog(@"%@",Data);
NSArray *list=Data[@"list"];
// NSLog(@"%@",list);
// 字典转模型
for (int i=0; i < list.count; i++) {
NSDictionary *dic=list[i];
recommendModel *mo=[[recommendModel alloc] init];
mo.myname=dic[@"myname"];
mo.smallpic=dic[@"smallpic"];
mo.gps=dic[@"gps"];
mo.flv=dic[@"flv"];
[self.MutableArray addObject:mo];
NSLog(@"%@",self.MutableArray);
}
//主线程刷新
dispatch_async(dispatch_get_main_queue(), ^{
//回调或者说是通知主线程刷新,
[self.CollectionView reloadData];
});
}];
}
-(void)collection {
UICollectionViewFlowLayout *CollectionFlowLayout=[[UICollectionViewFlowLayout alloc] init];
CollectionFlowLayout.itemSize=CGSizeMake(SYwidth/2-10, SYRealValueH(193));
CollectionFlowLayout.sectionInset=UIEdgeInsetsMake(6, 5, 5, 5);
CollectionFlowLayout.scrollDirection=UICollectionViewScrollDirectionVertical;
CollectionFlowLayout.minimumLineSpacing=10;
CollectionFlowLayout.minimumInteritemSpacing=10;
self.CollectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(0, SYRealValueH(64), SYwidth, SYheight - SYRealValueH(113)) collectionViewLayout:CollectionFlowLayout];
self.CollectionView.dataSource=self;
self.CollectionView.delegate=self;
[self.view addSubview:self.CollectionView];
[self.CollectionView registerClass:[videoCollectionViewCell class] forCellWithReuseIdentifier:@"MyCell"];
self.CollectionView.backgroundColor=[UIColor whiteColor];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.MutableArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
videoCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
recommendModel *mo=self.MutableArray[indexPath.row];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:mo.smallpic]];
//image与data的相互转换
UIImage *image = [UIImage imageWithData:data];
cell.headportraitImage.image=image;
cell.ageLabel.text=mo.myname;
cell.stateLabel.text=mo.gps;
// NSLog(@"%@",mo.gps);
return cell;
}