UICollectionView的使用方法

在项目开发中,我们经常需要实现一些特殊的排列方式,并且对于其排列方式还经常需要有更换的需求,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区域内需要显示的itemlayoutAttributes

- (NSArray* )layoutAttributesForElementsInRect:(CGRect)rect {

    NSLog(@"%@", NSStringFromCGRect(rect));

    

    NSMutableArray* layoutAttributes = [[NSMutableArray alloc] initWithCapacity:0];

    

    //遍历全的item位置,判断itemframerect是否相交,有重合的地方,如果有,就放入数组,返回

    for (UICollectionViewLayoutAttributes* attributes in _allLayoutAttributes) {

        //判断两个矩形是否相交

        if (CGRectIntersectsRect(attributes.frame, rect)) {

            [layoutAttributes addObject:attributes];

        }

    }

    

    return layoutAttributes;

}


在controller中的ViewDidLoad方法中创建UICollectionView,创建过程中需要一个layout,加上我们上面创建的就好了,同样collectionView需要实现两个代理方法 < UICollectionViewDelegate , UICollectionViewDataSource >
例子:

- (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;

    }

}


自定义item的方式和tableView一样的



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值