Model.h

View.h

view.m


#import "yiViewController.h"
#import "JDCollectionViewCell.h"
#import "JDModel.h"
#import "VideoViewController.h"
@interface yiViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
{
NSMutableArray*_dataArray;
}
@end
// 声明三个重用修饰符.
staticNSString*reuseID =@"cell";
@implementationyiViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 创建流式布局对象
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
// 设置单元格的大小
flowLayout.itemSize=CGSizeMake(198,260);
// 设置滚动方向
flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
// 设置最小列间距
flowLayout.minimumInteritemSpacing = 5;
// 设置最小行间距
flowLayout.minimumLineSpacing=50;
// 设置分区间距
flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
// 创建网格对象(网格视图.)
UICollectionView *cv = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 120, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flowLayout];
// 设置代理
cv.delegate=self;
cv.dataSource=self;
// 设置分页滚动
// cv.pagingEnabled = YES;
cv.backgroundColor = [UIColor whiteColor];
// 将网格添加到视图上
[self.view addSubview:cv];
//注册XIB方法
[cvregisterNib:[UINib nibWithNibName:@"JDCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:reuseID];
_dataArray = [NSMutableArray new];
for(inti =0; i<4; i++) {
JDModel*model = [JDModelnew];
//image XIB.h 控件属性
if(i%4==0) {
model.image=@"1";
}elseif(i%4==1){
model.image=@"2";
}elseif(i%4==2){
model.image=@"3";
}else{
model.image=@"4";
}
[_dataArrayaddObject:model];
}
}
// 设置多少个item
- (NSInteger)collectionView:(UICollectionView*)collectionView numberOfItemsInSection:(NSInteger)section
{
return _dataArray.count;
}
// 设置 cell
// 表格里是row
// 网格里是item
- (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath
{
// 根据可重用标识符查找cell
JDCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseID forIndexPath:indexPath];
JDModel*mod =_dataArray[indexPath.row];
cell.model= mod;
returncell;
}
-(void)collectionView:(UICollectionView*)collectionView didSelectItemAtIndexPath:(NSIndexPath*)indexPath{
VideoViewController *video = [VideoViewController new];
[self.navigationController pushViewController:video animated:YES];
}
@end
网友评论