在项目开发中,我们经常需要实现一些特殊的排列方式,并且对于其排列方式还经常需要有更换的需求,UICollectionView可以满足我们。看看该怎么实现吧。
1.UICollectionView和UITableView是很类似的,包括其代理方法。在collectionView中,没有行的概念,只有section和item的概念,当我们想实现某种排列方式的collectionView时,我们需要先实现一种排列方式(layout),我们先创建一个继承于UICollectionViewLayout的类,然后重写这个类的三个方法:
- (void)prepareLayout:
这个方法是比较重要的,也是比较核心的(个人认为),因为在这个方法里面我们需要设置所有需要展示出来的item的位置和大小,例子:
- (void)prepareLayout
{
_rect = [UIScreen mainScreen].bounds;
_allLayoutAttributes = [NSMutableArray arrayWithCapacity:0];
long items = [self.collectionView numberOfItemsInSection:0];
for (int i = 0; i < items; i ++) {
NSIndexPath* indexPath = [NSIndexPath indexPathForItem:i inSection:0];
UICollectionViewLayoutAttributes* attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
[_allLayoutAttributes addObject:attributes];
if (i == 0) {
attributes.frame = CGRectMake(0, 0, _rect.size.width, (_rect.size.height - 68) * 0.25);
_height = CGRectGetMaxY(attributes.frame);
}else{
if ((i % 2) == 1) {
attributes.frame = CGRectMake(0, _height + 0.5, _rect.size.width / 2.0 - 0.25, (_rect.size.height - 68 - 1.5) * 0.25);
// _height = CGRectGetMaxY(attributes.frame);
}else{
attributes.frame = CGRectMake(_rect.size.width / 2.0 + 0.5, _height + 0.5, _rect.size.width / 2.0 - 0.25, (_rect.size.height - 68 - 1.5) * 0.25);
_height = CGRectGetMaxY(attributes.frame);
}
}
}
}
- (CGSize)collectionViewContentSize:
这个方法是用来返回collectionView可滑动的范围的,例子:
//返回UICollectionView可滑动区域
- (CGSize)collectionViewContentSize {
//如果高度为0.collectionView不能滑动
_height = _height == 0 ? self.collectionView.bounds.size.height + 1 : _height;
return CGSizeMake(CGRectGetWidth(self.collectionView.bounds), _height);
}
- (NSArray* )layoutAttributesForElementsInRect:(CGRect)rect:
这个方法是用来返回所有rect范围内的item的layoutAttributes,例子:
//返回Rect区域内需要显示的item的layoutAttributes
- (NSArray* )layoutAttributesForElementsInRect:(CGRect)rect {
NSLog(@"%@", NSStringFromCGRect(rect));
NSMutableArray* layoutAttributes = [[NSMutableArray alloc] initWithCapacity:0];
//遍历全的item位置,判断item的frame和rect是否相交,有重合的地方,如果有,就放入数组,返回
for (UICollectionViewLayoutAttributes* attributes in _allLayoutAttributes) {
//判断两个矩形是否相交
if (CGRectIntersectsRect(attributes.frame, rect)) {
[layoutAttributes addObject:attributes];
}
}
return layoutAttributes;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[super initLeftBarButtonItemAndWithTitle:BLOOD_P_NAME];
//创建collectionView
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, mainWidth, mainHeight - 68) collectionViewLayout:[[BloodBannerLayout alloc]init]];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
self.collectionView.backgroundColor = [UIColor colorWithRed:232/255.0 green:232/255.0 blue:232/255.0 alpha:1];
[self.view addSubview:self.collectionView];
//注册item
[self.collectionView registerNib:[UINib nibWithNibName:@"BloodBannerCell" bundle:nil] forCellWithReuseIdentifier:bannerID];
[self.collectionView registerNib:[UINib nibWithNibName:@"FunctionButtonCell" bundle:nil] forCellWithReuseIdentifier:functionButtonID];
titleArray = @[@"血压测量",@"动态监护",@"血管管理",@"专家分析",@"健康档案",@"消息"];
bgImageArray = @[@"bloodPressure",@"activeLooking",@"bloodPressureControl",@"ecpertAnalysis",@"healthDoc",@"message"];
}
#pragma mark =========ICollectionViewDelegate,UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 7;
}
-(UICollectionViewCell* )collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell* cell = nil;
if (indexPath.item == 0) {
BloodBannerCell* cell1 = [collectionView dequeueReusableCellWithReuseIdentifier:bannerID forIndexPath:indexPath];
cell = cell1;
}else{
FunctionButtonCell* cell2 = [collectionView dequeueReusableCellWithReuseIdentifier:functionButtonID forIndexPath:indexPath];
cell2.titleImageView.image = [UIImage imageNamed:[bgImageArray objectAtIndex:indexPath.item - 1]];
// cell2.titleLable.text = [titleArray objectAtIndex:indexPath.item - 1];
cell = cell2;
}
return cell;
}
//点击跳转页======(血压测量,动态监护,血压管理,专家分析,健康档案,消息)
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.item) {
default:
break;
}
}