UICollectionView流式布局

摘要: 然后向ViewController加UICollectionView,选定UICollectionView,电脑鼠标鼠标右键按住,拖至MainVC.:按图中在建三个文档:GoodsCollectionViewCell.xib的排版设计为:Class为:GoodsCollectionViewCell也有目标...

然后向ViewController加UICollectionView,选定UICollectionView,电脑鼠标鼠标右键按住,拖至MainVC.:
按图中在建三个文档: GoodsCollectionViewCell.xib的排版设计为: Class为:GoodsCollectionViewCell 也有目标引入也一并搞好,最后 GoodsCollectionViewCell.h编码为: #import
@interface GoodsCollectionViewCell : UICollectionViewCell
{
}
@property (nonatomic, copy) NSString *photoPath;
@property (nonatomic, copy) NSString *goodsName;
@property (nonatomic, copy) NSString *digest;
@property (nonatomic, copy) NSString *price;
@property (nonatomic, copy) NSString *priceMarket;
GoodsCollectionViewCell.m编码为: #import "GoodsCollectionViewCell.h"
#define BASEURL "/拼凑照片的名字
NSString *fileName = [NSString stringWithFormat: @"%s%@",BASEURL,_photoPath];
NSURL *imageUrl = [NSURL URLWithString:fileName];
_imgPhotoPath.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageUrl]];
}
}
-(void)setGoodsName:(NSString *)goodsName {
if (_goodsName != goodsName) {
_goodsName = [goodsName copy];
_lbGoodsName.text=_goodsName;
}
}
-(void)setDigest:(NSString *)digest {
if (_digest != digest) {
_digest = [digest copy];
_lbDigest.text=_digest;
}
}
-(void)setPrice:(NSString *)price {
if (_price != price) {
_price = [price copy];
_lbPrice.text=_price;
}
}
-(void)setPriceMarket:(NSString *)priceMarket {
if (_priceMarket != priceMarket) {
_priceMarket = [priceMarket copy];
_lbPriceMarket.text=_priceMarket;
}
}
//这一涵数需轻载,要不然cell无法显示
-(instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self = [[NSBundle mainBundle]loadNibNamed:@"GoodsCollectionViewCell" owner:self options:nil].lastObject;
}
return self;
}


#define CELL_WIDTH 190 //collectViewCell总宽
#define CELL_HEIGHT 290 //collectViewCell高宽比
#define BASEURL "/UICollectionViewDataSource,UICollectionViewDelegate得加上 @interface MainVC ()
@property (weak, nonatomic) IBOutlet UICollectionView *cvGoods;
@property (strong,nonatomic) NSMutableArray *arrHeight;//iew上每一个item的高宽比,item里放CollectViewCell
@property (strong,nonatomic) NSMutableArray *arrPhotoPath;//产品照片的数字能量数组
@property (strong,nonatomic) NSMutableArray *arrGoodsName;
@property (strong,nonatomic) NSMutableArray *arrDigest;
@property (strong,nonatomic) NSMutableArray *arrPrice;
@property (strong,nonatomic) NSMutableArray *arrPriceMarket;
@end
@implementation MainVC
-(void)viewDidLoad{
[super viewDidLoad];
self.arrHeight = [NSMutableArray array];//一定要原始化,要不然后边的【self.arrHeight addObject】加不进值
self.arrPhotoPath = [NSMutableArray array];
self.arrGoodsName = [NSMutableArray array];
self.arrDigest = [NSMutableArray array];
self.arrPrice = [NSMutableArray array];
self.arrPriceMarket = [NSMutableArray array];
//这儿仅仅给数字能量数组原始化,对于数字能量数组中存的数据信息,你自身完成了
}
#pragma mark -
//回到section的数量,填1就可以
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
//回到相匹配section的item数量
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return self.arrGoodsName.count;//数字能量数组的原素数量,便是大家要界定的item数量
}
//定cell的宽和高
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
//CGFloat height=100+(arc4random()%100);任意的高,检测用
//给存cell高宽比的数字能量数组取值
[self.arrHeight addObject:[NSString stringWithFormat:@"%d",CELL_HEIGHT]];
//NSLog(@"arrHeight=%@",self.arrHeight);
return  CGSizeMake(CELL_WIDTH, CELL_HEIGHT);
}
//回到相匹配item的UICollectionviewCell,这一涵数会在每加一个产品时开启一次,一次加一个产品
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
//这一cell便是上边大家在xib文档里界定的
GoodsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GoodsCollectionViewCell" forIndexPath:indexPath];
cell.photoPath=self.arrPhotoPath[indexPath.row];//indexPath.row了解成当今数据库索引更强,之上面界定的数字能量数组原素相匹配。按英语含意反倒不太好了解,不知道老外是如何想的
cell.goodsName=self.arrGoodsName[indexPath.row];
cell.digest=self.arrDigest[indexPath.row];
cell.price=self.arrPrice[indexPath.row];
cell.priceMarket=self.arrPriceMarket[indexPath.row];
//设定cell情况
cell.backgroundColor = [UIColor whiteColor];//[UIColor colorWithRed:256/255.0 green:256/255.0 blue:256/255.0 alpha:1.0];
// 设定圆弧
cell.layer.cornerRadius = 5.0;
cell.layer.masksToBounds = YES;
//流式布局设定,即:一行多列,Y座标定在最少的地区
NSInteger remainder=indexPath.row%2;
NSInteger currentRow=indexPath.row/2;
CGFloat   currentHeight=[self.arrHeight[indexPath.row] floatValue];
CGFloat positonX=0;
if(remainder==0){
positonX=12;
}else{
positonX=CELL_WIDTH+22;
}
CGFloat positionY=(currentRow+1)*10;
for (NSInteger i=0; i


联系我们

全国服务热线:4000-399-000 公司邮箱:343111187@qq.com

  工作日 9:00-18:00

关注我们

官网公众号

官网公众号

Copyright?2020 广州凡科互联网科技股份有限公司 版权所有 粤ICP备10235580号 客服热线 18720358503

技术支持:凡科小程序