实现该界面花了点功夫。
基本思路是通过实现 UICollectionViewDelegateFlowLayout 的代理方法
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;
以10个一循环返回 item 大小。另外通过代理方法
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
同样以10个一循环返回 item 的可供服用的样式。
配置 UICollectionView
- (void)initContentCollectionView {
// self.adScrollView.frame.origin.y + self.adScrollView.frame.size.height
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
//(cell宽高)
layout.itemSize = CGSizeMake(CELLWEIGHT, CELLHEIGHT);
layout.minimumLineSpacing = CELLLINESPACING;
layout.minimumInteritemSpacing = CELLINTERITEMSPACING;
//广告图宽高-下面还有
layout.headerReferenceSize = CGSizeMake(IOS_SCREEN_WIDTH, HEADADVIEWHEIGHT);
//section(上、左、下、右)
layout.sectionInset = SECTIONBORDER;
self.contentCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0 ,64.f , IOS_SCREEN_WIDTH, IOS_SCREEN_CONTENT_HEIGHT - BOTTOM_MENU_BAR_HEIGHT) collectionViewLayout:layout];
self.contentCollectionView.backgroundColor = LIGHT_GRAY;
self.contentCollectionView.delegate = self;
self.contentCollectionView.dataSource = self;
self.contentCollectionView.showsVerticalScrollIndicator = NO;
self.contentCollectionView.alwaysBounceVertical = YES;
//collectionView(上、左、下、右)
self.contentCollectionView.contentInset = COLLECTIONCONTENTINSET;
[self.contentCollectionView registerClass:[ViewHomeMenuCollectionCell class] forCellWithReuseIdentifier:@"cell"];
[self.contentCollectionView registerClass:[ViewHomeMenuCollectionCellText class] forCellWithReuseIdentifier:@"cellText"];
[self.contentCollectionView registerClass:[ViewHomeMenuCollectionCellImage class] forCellWithReuseIdentifier:@"cellImage"];
[self.contentCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headAdView"];
[self addSubview:self.contentCollectionView];
}
其中self.contentCollectionView.alwaysBounceVertical = YES;是为了避免数据过少导致无法下拉刷新。
[self.contentCollectionView registerClass:[ViewHomeMenuCollectionCell class] forCellWithReuseIdentifier:@"cell"];
[self.contentCollectionView registerClass:[ViewHomeMenuCollectionCellText class] forCellWithReuseIdentifier:@"cellText"];
[self.contentCollectionView registerClass:[ViewHomeMenuCollectionCellImage class] forCellWithReuseIdentifier:@"cellImage"];
[self.contentCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headAdView"];
注册三个 Cell 及作为 headView 的UICollectionReusableView
UICollectionView 的 content 布局抽象成为一个 UICollectionViewLayout 对象。 UICollectionViewFlowLayout 则是 UICollectionViewLayout 的一个子类。
layout.itemSize = CGSizeMake(CELLWEIGHT, CELLHEIGHT);可以实现设定 Cell 的大小。另外还有一个属性 estimatedItemSize 则是 iOS 8.0 之后实现动态修改 cell 高宽的关键。
minimumLineSpacing最小行距
minimumInteritemSpacing最小间距
headerReferenceSizeheader大小
sectionInsetsection 的上左下右边距,不包括 UICollectionReusableView
self.contentCollectionView.contentInset = COLLECTIONCONTENTINSET; 设置 collectionView content边距。
#pragma mark - UICollectionViewDelegateFlowLayout
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.row%10) {
case 0:
return CGSizeMake((IOS_SCREEN_WIDTH-24)*0.333333333, (IOS_SCREEN_WIDTH-26)*0.3*1.2);
break;
case 1:
return CGSizeMake((IOS_SCREEN_WIDTH-20)*0.666666667, (IOS_SCREEN_WIDTH-21)*0.3*1.2);
break;
case 5:
return CGSizeMake((IOS_SCREEN_WIDTH-20)*0.666666667, (IOS_SCREEN_WIDTH-21)*0.3*1.2);
break;
case 6:
return CGSizeMake((IOS_SCREEN_WIDTH-24)*0.333333333, (IOS_SCREEN_WIDTH-26)*0.3*1.2);
break;
default:
return CGSizeMake((IOS_SCREEN_WIDTH-24)*0.333333333, ((IOS_SCREEN_WIDTH-26)/3)*0.37);
break;
}
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *dct = self.menuResult[indexPath.row];
switch (indexPath.row%10) {
case 0: {
ViewHomeMenuCollectionCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
cell.titleLabel.text = dct[@"name"];
cell.backgroundColor = self.cellColorArray[indexPath.row % self.cellColorArray.count];
[cell.titleLabel sizeToFit];
return cell;
break;
}
case 1: {
ViewHomeMenuCollectionCellImage * cellImage = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellImage" forIndexPath:indexPath];
[cellImage.imageView setImageURL:[NSURL URLWithString:[self createPhoto:dct[@"photo"] width:cellImage.imageView.frame.size.width height:cellImage.imageView.frame.size.height]]];
return cellImage;
break;
}
case 5: {
ViewHomeMenuCollectionCellImage * cellImage = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellImage" forIndexPath:indexPath];
[cellImage.imageView setImageURL:[NSURL URLWithString:[self createPhoto:dct[@"photo"] width:cellImage.imageView.frame.size.width height:cellImage.imageView.frame.size.height]]];
return cellImage;
break;
}
case 6: {
ViewHomeMenuCollectionCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
cell.titleLabel.text = dct[@"name"];
cell.backgroundColor = self.cellColorArray[indexPath.row % self.cellColorArray.count];
[cell.titleLabel sizeToFit];
return cell;
break;
}
default: {
ViewHomeMenuCollectionCellText * cellText = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellText" forIndexPath:indexPath];
cellText.titleLabel.text = dct[@"name"];
return cellText;
break;
}
}
//
/* ViewHomeMenuCollectionCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
ViewHomeMenuCollectionCellText * cellText = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellText" forIndexPath:indexPath];
ViewHomeMenuCollectionCellImage * cellImage = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellImage" forIndexPath:indexPath];
NSDictionary *dct = self.menuResult[indexPath.row];
NSLog(@"%ld",indexPath.row%10);
switch (indexPath.row%10) {
case 0:
cell.titleLabel.text = dct[@"name"];
cell.backgroundColor = self.cellColorArray[indexPath.row % self.cellColorArray.count];
return cell;
break;
case 1:
[cellImage.imageView setImageURL:[NSURL URLWithString:[self createPhoto:dct[@"photo"] width:cellImage.imageView.frame.size.width height:cellImage.imageView.frame.size.height]]];
return cellImage;
break;
case 5:
[cellImage.imageView setImageURL:[NSURL URLWithString:[self createPhoto:dct[@"photo"] width:cellImage.imageView.frame.size.width height:cellImage.imageView.frame.size.height]]];
return cellImage;
break;
case 6:
cell.titleLabel.text = dct[@"name"];
cell.backgroundColor = self.cellColorArray[indexPath.row % self.cellColorArray.count];
return cell;
break;
default:
cellText.titleLabel.text = dct[@"name"];
return cellText;
break;
}*/
}
/**/注释当中的方法因为每次都复用三个 Cell,造成内存消耗不断增加。改为现在使用的方法则有改善。
http://www.devdiv.com/forum.php?mod=viewthread&tid=128378
http://zjqzy03080312.blog.163.com/blog/static/185742807201411411252280/
本文详细阐述了如何通过实现UICollectionViewDelegateFlowLayout代理方法来动态调整UICollectionView中item的大小,以及如何通过注册不同类型的UICollectionViewCell来实现高效的数据展示。同时,文章还介绍了如何避免数据过少导致的下拉刷新问题,并提供了优化内存消耗的解决方案。
704

被折叠的 条评论
为什么被折叠?



