先正常写表格UITableView,下面的是设置表格分组的样式,(不分组就把表格样式的改成indexPath.row就行了)
在表格内容里写:(就是创建网格必须的流水布局和设置网格里图片位置的东西)
tbv.rowHeight = 360;
//创建流水布局 确定单元格的位置
UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
//设置单元格大小
flow.itemSize = CGSizeMake((self.view.frame.size.width - 4)/4, (360 - 3)/3);
//设置分区间距 (上边距,左,下,右)
flow.sectionInset = UIEdgeInsetsMake(30, 80, 10, 80);
//设置最小的行间距
flow.minimumLineSpacing = 0.5;
//设置最小的列间距
flow.minimumInteritemSpacing = 1;
//创建网格视图
collect = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 360) collectionViewLayout:flow];
collect.delegate = self;
collect.dataSource = self;
collect.backgroundColor = [UIColor whiteColor];
//注册单元格
[collect registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuse];
[cell addSubview:collect];
然后就是正常写网格里的东西了:
记得最上面@end下边写:
NSString *reuse = @"cell";
//这是设置网格里共有几个图的
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 4;
}
//这是设置网格里cell内容的(主要添加图片的)注意是:(UICollectionView )
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuse forIndexPath:indexPath];
//设置 cell 内容
cell.backgroundColor = [UIColor darkGrayColor];
UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, (self.view.frame.size.width - 4)/4, (360 - 3)/3)];
imgV.image = [UIImage imageNamed:@"勾选的副本"];
[cell addSubview: imgV];
//返回cell
return cell;
}
最后是效果图:
网友评论