iOS|iOS - UICollectionView

注意点 【iOS|iOS - UICollectionView】1,滚动到指定位置

[_cv scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:8 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];

1,直接拖UICollectionView控件,默认是flow布局,设置代理UICollectionViewDelegateFlowLayout,UICollectionViewDataSource 2,代码创建
- (void)createCollectionView{ //关闭自适应self.automaticallyAdjustsScrollViewInsets = NO; _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, 200) collectionViewLayout:self.flowLayout]; _collectionView.dataSource = self; _collectionView.delegate = self; _collectionView.backgroundColor = [UIColor colorWithRed:237/255.0 green:237/255.0 blue:237/255.0 alpha:0.8 ]; [self.headerBgView addSubview: _collectionView]; //注册cell[_collectionView registerNib:[UINib nibWithNibName:FirstCategoryCellID bundle:nil] forCellWithReuseIdentifier:FirstCategoryCellID]; }

- (UICollectionViewFlowLayout *)flowLayout { if (!_flowLayout) { _flowLayout = [[UICollectionViewFlowLayout alloc]init]; //设置单元格大小 _flowLayout.itemSize = CGSizeMake(60, 90); //最小行间距(默认为10) _flowLayout.minimumLineSpacing = 10; //最小item间距(默认为10) _flowLayout.minimumInteritemSpacing = 10; //设置senction的内边距 _flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10); //设置UICollectionView的滑动方向 _flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal; } return _flowLayout; }

2,基本的代理方法
#pragma mark UICollectionViewDelegate-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{ return 1; } -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return 7; }

3,设置cell
//Item间距 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section { return 5; } //Item行间距 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { return 10; } //设置cell的size - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {HotSearchModel *model = self.hotSearchArr[indexPath.row]; NSLog(@"---%f--",[self getMultiLineWithFont:14 andText:model.text].width); return [self getMultiLineWithFont:14 andText:model.text]; } //计算lable的size - (CGSize)getMultiLineWithFont:(CGFloat )font andText:(NSString *)text { UILabel *lable = [[UILabel alloc]init]; lable.text = text; lable.numberOfLines = 1; //多行显示,计算高度 CGSize size= [lable.text boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 20, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font]} context:nil].size; return CGSizeMake(size.width+20, size.height+5); } //cell -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { HotSearchCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:HotSearchCellID forIndexPath:indexPath]; [cell setHotSearchText:self.hotSearchArr[indexPath.row].text]; return cell; }

另一种
代理 #pragma mark - UICollectionViewDelegateFlowLayout-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return CGSizeMake(self.myCollection.frame.size.width/2 - 5, 55); }#pragma mark - collection delegate-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; }-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.dataSource.count; }-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { NSTCollectionCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath]; [cell setUPModel:[self.dataSource objectAtIndex:indexPath.row]]; return cell; }-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { NSTCollectionCell * cell = (NSTCollectionCell *)[collectionView cellForItemAtIndexPath:indexPath]; if(![self.lastCell isEqual:cell]) { [self.lastCell selectModel]; self.lastCell = cell; } else self.lastCell = nil; [UIView animateWithDuration:0.2 animations:^{ [self.NextBtn setBackgroundColor:self.lastCell ? color_green : color_btn_gray]; }]; self.NextBtn.enabled = self.lastCell ? YES:NO; [cell selectModel]; }

    推荐阅读