标签:indexPath 01 layout UICollectionView self 布局 collectionView CGFloat
一,代码:
1.布局方式设置,创建UICollectionView
- (void)initailContentView { //导航 self.navigationBar = ({ CGFloat X = 0.0f; CGFloat Y = 0.0f; CGFloat W = [UIScreen mainScreen].bounds.size.width; CGFloat H = 44.f; UIView *view = [[UIView alloc]initWithFrame:CGRectMake(X, Y, W, H)]; view; }); [self.view addSubview:self.navigationBar]; //表视图 self.listView = ({ CGFloat X = 0.0f; CGFloat Y = CGRectGetMaxY(self.navigationBar.frame); CGFloat W = [UIScreen mainScreen].bounds.size.width; CGFloat H = ([UIScreen mainScreen].bounds.size.height - Y); //初始化layout UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init]; //设置布局方式为竖直布局 系统默认竖直布局 layout.scrollDirection = UICollectionViewScrollDirectionVertical; //设置同一列中间隔的cell最小间距 layout.minimumInteritemSpacing = 5.f; //设置最小行间距 layout.minimumLineSpacing = 5.f; //每个分区的上,左,下,右的缩进量 layout.sectionInset = UIEdgeInsetsMake(10.f, 10.f, 10.f, 10.f); //设置每个item的大小 CGFloat itemW = (W - layout.minimumLineSpacing - layout.sectionInset.left - layout.sectionInset.right)/2.f; CGFloat itemH = 80.f; layout.itemSize = CGSizeMake(itemW, itemH); //创建UICollectionView UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(X, Y, W, H) collectionViewLayout:layout]; collectionView.delegate = self; collectionView.dataSource = self; collectionView.showsVerticalScrollIndicator = NO; collectionView.showsHorizontalScrollIndicator = NO; collectionView.backgroundColor = [UIColor whiteColor]; collectionView; }); [self.view addSubview:self.listView]; //注册cell [self.listView registerClass:CustomCollectionViewCell.class forCellWithReuseIdentifier:NSStringFromClass(CustomCollectionViewCell.class)]; //注册header [self.listView registerClass:CustomCollectionReusableView.class forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CustomCollectionReusableViewHeader"]; }
2.遵循代理 并实现数据源代理方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return self.dataSource.count; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.dataSource[section].items.count; } - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { CustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(CustomCollectionViewCell.class) forIndexPath:indexPath]; cell.model = self.dataSource[indexPath.section].items[indexPath.item]; return cell; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { } ///分区头 - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { if ([kind isEqualToString:UICollectionElementKindSectionHeader]) { CustomCollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CustomCollectionReusableViewHeader" forIndexPath:indexPath]; header.title = self.dataSource[indexPath.section].name; return header; } else { return [CustomCollectionReusableView new]; } } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section { return CGSizeZero; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section { return CGSizeMake(CGRectGetWidth(collectionView.frame), 40.f); }
3.自定义UICollectionViewCell
#pragma mark - Initail Methods - (void)initailContentView { self.titleLbl = ({ CGFloat X = 0.0f; CGFloat W = CGRectGetWidth(self.frame); CGFloat H = 20.f; CGFloat Y = (CGRectGetHeight(self.frame) - H); UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(X, Y, W, H)]; lbl.textColor = [UIColor blackColor]; lbl.font = [UIFont systemFontOfSize:15 weight:UIFontWeightRegular]; lbl; }); [self addSubview:self.titleLbl]; self.headerView = ({ CGFloat X = 0.0f; CGFloat Y = 0.0f; CGFloat W = CGRectGetWidth(self.frame); CGFloat H = CGRectGetMinY(self.titleLbl.frame); UIView *view = [[UIView alloc]initWithFrame:CGRectMake(X, Y, W, H)]; view.backgroundColor = [UIColor blueColor]; view; }); [self addSubview:self.headerView]; } - (void)setModel:(CustomItemModel *)model { _model = model; self.titleLbl.text = model.name; }
二,示例/demo
标签:indexPath,01,layout,UICollectionView,self,布局,collectionView,CGFloat 来源: https://www.cnblogs.com/lxlx1798/p/13190844.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。