iOS collectionview

UICollectionViewDelegate, UICollectionViewDataSource实现了这两个代理

//collectionview相关
- (void)setCollectionviewLayout {
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    [flow setScrollDirection:UICollectionViewScrollDirectionVertical];//竖直滑动
//    flow.minimumLineSpacing = 0;
//    flow.minimumInteritemSpacing = 0;
    flow.itemSize = CGSizeMake(75, 120);

    self.typeCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(15, 0, Screen_Width-30, Screen_Height-49-64) collectionViewLayout:flow];
    self.typeCollection.backgroundColor = [UIColor clearColor];
    self.typeCollection.delegate = self;
    self.typeCollection.dataSource = self;
    self.typeCollection.showsHorizontalScrollIndicator = NO;
    self.typeCollection.showsVerticalScrollIndicator = NO;
    [self.typeCollection registerNib:[UINib nibWithNibName:@"TypeCell" bundle:nil] forCellWithReuseIdentifier:@"TypeCell"];//注册cell
    [self.view addSubview:self.typeCollection];
    [self.typeCollection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];//注册分区头
}

#pragma mark - collection
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
//    return self.treeList.count;
    return 9;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    TypeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TypeCell" forIndexPath:indexPath];
//    [cell setDateWithString:self.treeList[indexPath.row]];
    return cell;
}

//返回分区header
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, 30)];
    label.text = @"header";
    UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
    [view addSubview:label];
    return view;
}

//分区header的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(Screen_Width, 30);
}

你可能感兴趣的:(iOS collectionview)