iOSUICollectionView使用

iOSUICollectionView使用

2023年7月14日发(作者:)

iOSUICollectionView使⽤ //创建⼀个layout布局类(也可以⾃定义,下篇中会有) UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init]; //设置布局⽅向为垂直流布局 Direction = UICollectionViewScrollDirectionVertical; //设置布局⽅向为垂直流布局 Direction =UICollectionViewScrollDirectionHorizontal; //设置每个item的⼤⼩为100*100 ze = CGSizeMake(100, 100); //设置⾏间距 mLineSpacing; //设置列间距 mInteritemSpacing;(根据⽗控件的宽来设置,需要计算) //创建collectionView 通过⼀个布局策略layout来创建 UICollectionView * collect = [[UICollectionView alloc]initWithFrame: collectionViewLayout:layout]; //代理设置 te=self; urce=self;

//注册item类型 这⾥使⽤系统的类型 [collect registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellid"]; [ addSubview:collect];⼩结下:-- sizeForItemAtIndexPath⽅法是设置cell的宽⾼,-- insetForSectionAtIndex⽅法是设置⼀个section在CollectionView中的内边距;-- minimumInteritemSpacingForSectionAtIndex⽅法是设置cell之间的最⼩边距(我下⾯会详细讲解这个⽅法);-- minimumLineSpacingForSectionAtIndex⽅法是设置每⾏之间的距离;

注:必须实现的两个⽅法 //返回每个分区的item个数-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

return 10;}//返回每个item-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellid" forIndexPath:indexPath];

oundColor = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1];

return cell;}// 两个cell之间的最⼩间距,是由API⾃动计算的,只有当间距⼩于该值时,cell会进⾏换⾏(列间距)- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayoutminimumInteritemSpacingForSectionAtIndex:(NSInteger)section{ return 15;}

// 两⾏之间的最⼩间距(⾏间距)- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayoutminimumLineSpacingForSectionAtIndex:(NSInteger)section{ return 30;}// 该⽅法是设置⼀个section的上左下右边距(返回的间距是整体collectionView距离⽗控件之间的间距)- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayoutinsetForSectionAtIndex:(NSInteger)section{ // 注意,这⾥默认会在top有+64的边距,因为状态栏+导航栏是64. // 因为我们常常把[[UIScreen mainScreen] bounds]作为CollectionView的区域,所以苹果API就默认给了+64的EdgeInsets,这⾥其实是⼀个坑,⼀定要注意。 // 这⾥我暂时不⽤这个边距,所以top减去64 // 所以这是就要考虑你是把Collection从屏幕左上⾓(0,0)开始放还是(0,64)开始放。 return UIEdgeInsetsMake(10 10, 10, 10);}- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{ return CGSizeMake(90, 90);}

发布者:admin,转转请注明出处:http://www.yc00.com/web/1689265174a226455.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信