iOS --- UIcollectionView设置头尾

时间:2025-01-30 08:05:05

- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view.

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

 

}

#pragma mark - datesource


//item个数

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

{

    return 10;

}

// 这里面我们使用了自定义的cell类,所以,我们要创建这样的一个类  



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

{

    MyCollectionCell *cell  = [collectionView dequeueReusableCellWithReuseIdentifier:keyforIndexPath:indexPath];//不会为空,若为空自动创建了

//    contentView  property

//    backgroundView  property

//    selectedBackgroundView  property

//    Managing the Cell’s State

//    selected  property

//    highlighted  property

    cell.contentView.backgroundColor = [UIColor yellowColor];

    cell.label.text = [NSString stringWithFormat:@"%d %d", indexPath.section,indexPath.row];

    return cell;

 

}

//分区数

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView

{

    return 10;

}

//step2 设置分区header 和 footer 的第二步 :重用 此处我们用到了Header类和Footer类是需要我们自行创建的









- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath

{

    if (kind == UICollectionElementKindSectionHeader) {

        HeaderView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kindwithReuseIdentifier:key1 forIndexPath:indexPath];//需要zhuce

        header.label.text = @"AAA";

        return header;

    }else{

        foolter *footer = [collectionView dequeueReusableSupplementaryViewOfKind:kindwithReuseIdentifier:key2 forIndexPath:indexPath];

        footer.label.text = @"BBB";//step2:

        return footer;

    }

 

}

#pragma mark - delegate

// step3 : 设置分区header 和 footer 第三步:指定大小://设置header的高度(注册,重用,指定大小)

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section

{

    return CGSizeMake(32060);//宽默认

}

//step3 指定大小://设置footer的高度

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section

{

  return CGSizeMake(10060);//宽默认

}


//上下间距

//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section

//{

//    return 10;

//}

//行间距

//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section

//{

//    return 100;

//}

//每个视图的宽高

//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

//{

//    return CGSizeMake(100, 100);

//}


设置整个分区相对上下左右的间距

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

//{

//    return UIEdgeInsetsMake(50, 50, 50, 50);

//}

 

@end

运行效果如下图: