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)。







  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS中,我们可以使用滚动视图(UIScrollView)来创建一个可以进行滚动的内容视图。在该滚动视图中添加按钮,可以通过以下步骤实现: 1. 创建滚动视图对象:使用`UIScrollView`类创建一个滚动视图对象,可以通过代码或者图形化界面编辑器进行创建。 2. 设置滚动范围:通过设置滚动视图的`contentSize`属性,确定滚动内容的大小。内容大小应该大于滚动视图的可见区域,这样才能进行滚动。例如,`scrollView.contentSize = CGSizeMake(320, 600)`。 3. 添加按钮:创建按钮对象,并设置按钮的位置和样式。例如,`UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(50, 50, 100, 50)]`。 4. 将按钮添加到滚动视图中:使用`addSubview`方法将按钮对象添加到滚动视图中。例如,`[scrollView addSubview:button]`。 5. 设置按钮的位置:通过设置按钮的`frame`属性,确定按钮在滚动视图中的位置。需要注意的是,按钮的位置是相对于滚动视图的坐标系的。例如,`button.frame = CGRectMake(50, 50, 100, 50)`。 6. 设置滚动视图的代理:如果需要监听按钮的点击事件,可以将滚动视图的`delegate`属性设置为当前的视图控制器,并实现`UIScrollViewDelegate`协议。例如,`scrollView.delegate = self`。 7. 监听按钮的点击事件:在滚动视图的代理方法中,根据需要实现按钮的点击事件处理逻辑。例如,对于点击事件的处理可以通过`UIButton`的`addTarget:action:forControlEvents:`方法或者手势识别器来完成。 以上就是在iOS中使用滚动视图添加按钮的基本步骤。根据需要,可以根据具体情况进行定制和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值