iOS集合视图学习笔记

UICollectionView 和 UICollectionViewController 类是iOS6 新引进的API,用于展示集合视图,布局更加灵活,可实现多列布局,用法类似于UITableView 和 UITableViewController 类。
使用UICollectionView 必须实现UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout这三个协议。




<UICollectionVIewDataSource> 举例:

 

//section 的数量
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return self.numberOfSections;
}

//section 中 items的数量
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return self.itemsInSection;
}
//配置items
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    cell.selectedBackgroundView = [[UIView alloc]initWithFrame:CGRectZero];
    cell.selectedBackgroundView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.5f];
    
    
    // Configure the cell
    
    return cell;
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if(kind == UICollectionElementKindSectionHeader){
        UICollectionReusableView *header = [self.collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        header.backgroundColor = [UIColor blackColor];
        return header;
    }
    else if(kind == UICollectionElementKindSectionFooter){
        UICollectionReusableView *footer = [self.collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer" forIndexPath:indexPath];
        footer.backgroundColor = [UIColor blackColor];
        return footer;
    }
    return nil;
}
<UICollectionViewDelegate> 举例:

//选择item
-(void)collectionView:(UICollectionView *)aCollectionView didSelectItemAtIndexPath:(nonnull NSIndexPath *)indexPath{
    NSLog(@"Selected %@",indexPath);
}
//取消选择item
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"Deselected %@",indexPath);
}


//item高亮
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}



//选择item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}



//是否展示菜单
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}
//委托是否能执行给定的操作
- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}
//执行给定的操作
- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}


<UICollectionViewDelegateFlowLayout> 举例:

//header的尺寸
-(CGSize)collectionView:(UICollectionView *)collectionView
                 layout:(nonnull UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return self.useHeaders?CGSizeMake(60.0f, 30.0f):CGSizeZero;
}
//footer的尺寸
-(CGSize)collectionView:(UICollectionView *)collectionView
                 layout:(nonnull UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    return self.useFooters?CGSizeMake(60.0f, 30.0f):CGSizeZero;
}


如何建立集合视图?

1.通过控制器使用集合视图

构建集合视图控制器需要提供并设置好布局。

集合视图控制器宣称自己实现了<UICollectionViewDataSource,UICollectionViewDelegate>

如果使用的是流式布局则也会宣称实现了 <UICollectionViewDelegateFlowLayout>

UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
TestBedViewController *tbvc = [[TestBedViewController alloc]initWithCollectionViewLayout:layout];</span>


2.直接使用集合视图

我们需要用布局对象来创建好集合视图,并设置集合视图的数据源和委托。

UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
collectionView.dataSource = self;
collectionView.delegate = self;
  


可以定制的布局属性包括标准的布局元素(frame,center,size)、透明度(alpha和hidden)、z轴位置(zIndex)、以及坐标变换的方式(transform3d)。







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值