快速创建 collectionView

快速创建collectionView的代码
#pragma mark - 设置collectionView界面
- (void)setupUI {
    //1.初始化layout
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    //设置collectionView滚动方向
    //    [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    //设置headerView的尺寸大小
    layout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 44);
    //该方法也可以设置itemSize
    layout.itemSize =CGSizeMake(SCREEN_WIDTH / 3 - 1, 130);
    
    //2.初始化collectionView
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    [self.view addSubview:collectionView];
    collectionView.backgroundColor = [UIColor clearColor];
    
    //3.注册collectionViewCell
    //注意,此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致 均为 cellId
    [collectionView registerClass:[HDUICollectionViewCell class] forCellWithReuseIdentifier:cellId];
    
    //注册headerView  此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致  均为reusableView
    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView"];
    
    //4.设置代理
    collectionView.delegate = self;
    collectionView.dataSource = self;
    
    _collectionView = collectionView;
}


#pragma mark collectionView代理方法
//返回section个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    if (self.pictureCategoryModel) {
        return self.pictureCategoryModel.showapi_res_body.list.count;
    }
    else {
        return 0;
    }
}

//每个section的item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.pictureCategoryModel.showapi_res_body.list[section].list.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HDUICollectionViewCell *cell = (HDUICollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
    cell.title = self.pictureCategoryModel.showapi_res_body.list[indexPath.section].list[indexPath.row].name;
    cell.backgroundColor = [UIColor yellowColor];
    return cell;
}

//设置每个item的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(SCREEN_WIDTH / 3 - 1, 130);
}

//// footer的size
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
//    return CGSizeMake(10, 10);
//}
//
//// header的size
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
//    return CGSizeMake(SCREEN_WIDTH, 44);
//}

//设置每个item的UIEdgeInsets
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

//设置每个item水平间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}

//设置每个item垂直间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 1;
}

//通过设置SupplementaryViewOfKind 来设置头部或者底部的view,其中 ReuseIdentifier 的值必须和 注册是填写的一致,本例都为 “reusableView”
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView" forIndexPath:indexPath];
    
    // 此循环是解决组头视图复用问题
    for (UIView *view in headerView.subviews) {
        [view removeFromSuperview];
    }
    headerView.backgroundColor =[UIColor grayColor];
    UILabel *label = [[UILabel alloc] initWithFrame:headerView.bounds];
    for (NSInteger i = 0; i<self.pictureCategoryModel.showapi_res_body.list.count; i++) {
        if (indexPath.section==i) {
            label.text = self.pictureCategoryModel.showapi_res_body.list[indexPath.section].name;
        }
    }
    label.font = [UIFont systemFontOfSize:20];
    NSString *dLabelString = label.text;
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:dLabelString];  
    NSMutableParagraphStyle   *paragraphStyle   = [[NSMutableParagraphStyle alloc] init];
    //第一行头缩进
    [paragraphStyle setFirstLineHeadIndent:15.0];
    //行间距
    //[paragraphStyle setLineSpacing:5.0];
    //段落间距
    //[paragraphStyle setParagraphSpacing:10.0];
    //头部缩进  
    //[paragraphStyle setHeadIndent:15.0];  
    //尾部缩进  
    //[paragraphStyle setTailIndent:250.0];  
    //最小行高  
    //[paragraphStyle setMinimumLineHeight:20.0];  
    //最大行高  
    //[paragraphStyle setMaximumLineHeight:20.0];  
    
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [dLabelString length])];  
    [label setAttributedText:attributedString];
    [headerView addSubview:label];
    return headerView;
}

//点击item方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    HDUICollectionViewCell *cell = (HDUICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
//    NSString *msg = cell.title;
//    NSLog(@"%@",msg);
}

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 10,489评论 4 57
  • 翻译自“Collection View Programming Guide for iOS” 0 关于iOS集合视...
    lakerszhy阅读 3,373评论 1 22
  • 由于没有用过collectionview,也是刚开始纯代码布局,所以查了很多资料和问了一些朋友,总算解决问题 在v...
    丶逝水流年阅读 3,196评论 1 5
  • 精神病院的病房里,入住了一个新来的患者(我们姑且叫他A吧)。病房里原有一位老患者(我们姑且把他叫做B吧),正为没有...
    李野航阅读 385评论 1 3
  • 孤独大概就是每天在学校里,面对来来往往的人,却没有一个可以说心里话的人。 孤独大概就是每天一起吃饭上课的室友,也只...
    2301阅读 88评论 0 0