首页 > 其他 > 详细

UICollectionView

时间:2015-04-22 11:51:49      阅读:95      评论:0      收藏:0      [点我收藏+]

UICollectionView 类似于 UITableView ,布局更灵活。

//实现三个协议:
<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

//创建 UICollectionView
- (void)createCollectionView{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    //设置对齐方式
    [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    //cell间距
    layout.minimumInteritemSpacing = 5.0f;
    //cell行距
    layout.minimumLineSpacing = 1.0f;
    
    //collectionViewLunBo = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 20, 320, 200)];
    //需要layout 否则崩溃:UICollectionView must be initialized with a non-nil layout parameter
    collectionViewLunBo = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 20, 320, 300) collectionViewLayout:layout];
    collectionViewLunBo.dataSource = self;
    collectionViewLunBo.delegate = self;
    collectionViewLunBo.pagingEnabled = YES;
    collectionViewLunBo.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:collectionViewLunBo];
    //注册Cell类,否则崩溃: must register a nib or a class for the identifier or connect a prototype cell in a storyboard
    [collectionViewLunBo registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"MyCollectionViewCell"];
}

//required
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    //[collectionViewLunBo registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"MyCollectionViewCell"];
    
    static NSString *cellIDa = @"MyCollectionViewCell";
    collectionCell = (MyCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIDa forIndexPath:indexPath];
    collectionCell.backgroundColor = [UIColor blueColor];
    //collectionCell.imageView.image = [UIImage imageNamed:@""];
    return collectionCell;
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 4;
}

//optional
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%@",indexPath);
    UICollectionViewCell * cell = (UICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake(310, 300);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{

    return UIEdgeInsetsMake(5, 5, 5, 5);// top left bottom right  Cell边界范围
}


UICollectionView

原文:http://my.oschina.net/liuchuanfeng/blog/404901

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!