UICollectionView 不同大小Cell 等间距的实现
2015-05-12 17:17
225 查看
在IOS项目中有时会用到UICollectionView控件,大家应该都不太陌生,即便有没用过的同学,看一下教程也能轻松的掌握。不过要实现cell大小不同,又要等间距,应该怎么做呢?如下图:
一、首先需要创建EqualSpaceFlowLayout继承UICollectionViewFlowLayout,如下面的代码:
在EqualSpaceFlowLayout.m文件中重载- (void)prepareLayout方法实现每个cell的布局,如下
最后返回每个cell的layout情况
提供项目下载地址:http://download.csdn.net/detail/chchong1234/8692121
一、首先需要创建EqualSpaceFlowLayout继承UICollectionViewFlowLayout,如下面的代码:
#import <UIKit/UIKit.h> @protocol EqualSpaceFlowLayoutDelegate<UICollectionViewDelegateFlowLayout> @end @interface EqualSpaceFlowLayout : UICollectionViewFlowLayout @property (nonatomic,weak) id<EqualSpaceFlowLayoutDelegate> delegate; @end
在EqualSpaceFlowLayout.m文件中重载- (void)prepareLayout方法实现每个cell的布局,如下
#pragma mark - Methods to Override - (void)prepareLayout { [super prepareLayout]; NSInteger itemCount = [[self collectionView] numberOfItemsInSection:0]; self.itemAttributes = [NSMutableArray arrayWithCapacity:itemCount]; CGFloat xOffset = self.sectionInset.left; CGFloat yOffset = self.sectionInset.top; CGFloat xNextOffset = self.sectionInset.left; for (NSInteger idx = 0; idx < itemCount; idx++) { NSIndexPath *indexPath = [NSIndexPath indexPathForItem:idx inSection:0]; CGSize itemSize = [self.delegate collectionView:self.collectionView layout:self sizeForItemAtIndexPath:indexPath]; xNextOffset+=(self.minimumInteritemSpacing + itemSize.width); if (xNextOffset > [self collectionView].bounds.size.width - self.sectionInset.right) { xOffset = self.sectionInset.left; xNextOffset = (self.sectionInset.left + self.minimumInteritemSpacing + itemSize.width); yOffset += (itemSize.height + self.minimumLineSpacing); } else { xOffset = xNextOffset - (self.minimumInteritemSpacing + itemSize.width); } UICollectionViewLayoutAttributes *layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath]; layoutAttributes.frame = CGRectMake(xOffset, yOffset, itemSize.width, itemSize.height); [_itemAttributes addObject:layoutAttributes]; } }
最后返回每个cell的layout情况
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath { return (self.itemAttributes)[indexPath.item]; } - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect { return [self.itemAttributes filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(UICollectionViewLayoutAttributes *evaluatedObject, NSDictionary *bindings) { return CGRectIntersectsRect(rect, [evaluatedObject frame]); }]]; } - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds { return NO; }
提供项目下载地址:http://download.csdn.net/detail/chchong1234/8692121
相关文章推荐
- ios UICollectionView实现不同大小cell等间距
- Cocos2d-JS中CollectionView实现不同大小Cell同时显示的技巧(ListView中添加显示标签)
- 横向分页滚动的UICollectionView,cell左右排版 支持多组Cell实现。
- UICollectionview cell 行间距、列间距
- Android TextView 实现文字大小不同和文字颜色不同
- uicollectionview cell 行间距、列间距
- Android TextView 实现文字大小不同和文字颜色不同
- Textview增加图片并实现点击与文字设置不同大小颜色
- UICollectionViewCell等间距
- iOS UICollectionView cell 0间距问题的解决与分割线的设置
- 进击的KFC: OC实现UICollectionViewCell自适应文字宽度
- UICollectionView的cell间距问题
- UICollectionViewCell水平缩放,中间大两边小效果的实现
- Android TextView 实现文字大小不同和文字颜色不同
- iOS中UICollectionView实现单个cell滚动翻页并放大cell
- 设置UICollectionViewCell的尺寸/间距等
- Cocos2d-x 3.0 开发(十四)使用UIScrollView 实现大小不同物品拖动展示
- 实现UITableView中点击不同cell进入不同Controller的方法
- 用UICollectionView实现点击展开收缩的cell
- iOS关于在UITableView中,实现多个cell中不同的倒计时实现