美文网首页iOS自我学习库
ios的collectionView区头设置

ios的collectionView区头设置

作者: Amok校长 | 来源:发表于2016-08-27 13:03 被阅读6978次

    直接上代码了:

    #import "PhotoWallCell.h"

    #import "ViewController.h"

    #define kScreenWidth [UIScreen mainScreen].bounds.size.width

    #define kScreenHeight [UIScreen mainScreen].bounds.size.height

    static NSString *collectionCellIndentider = @"collectionCellIndentider";

    @interface ViewController ()

    @property(nonatomic,weak)UICollectionView*collectionView;

    @end

    @implementation ViewController

    - (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    [self.view addSubview:self.collectionView];

    }

    - (UICollectionView *)collectionView{

    if (_collectionView) {

    return _collectionView;

    }

    CGFloat collectionViewHeight = kScreenHeight - 190;

    CGRect frame = CGRectMake(0, 150, kScreenWidth, collectionViewHeight);

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];

    //设置区头的大小

    layout.headerReferenceSize = CGSizeMake(320, 30);

    //设置item大小

    layout.itemSize =CGSizeMake(100, 100);

    //设置区边间距

    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);

    //设置最小间距

    [layout setMinimumInteritemSpacing:10.0f];

    [layout setMinimumLineSpacing:10.0f];

    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:layout];

    collectionView.opaque = NO;

    collectionView.backgroundColor = self.view.backgroundColor;

    collectionView.dataSource = self;

    collectionView.delegate = self;

    collectionView.pagingEnabled = YES;

    collectionView.showsVerticalScrollIndicator = NO;

    collectionView.showsHorizontalScrollIndicator = NO;

    [self.view addSubview:collectionView];

    _collectionView = collectionView;

    /*!

    *  @brief  注册cell

    */

    [collectionView registerClass:[PhotoWallCell class] forCellWithReuseIdentifier:collectionCellIndentider];

    [collectionView registerClass:[PhotoWallCell class]  forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:collectionCellIndentider];

    return collectionView;

    }

    #pragma mark -

    #pragma mark -cell Delegate

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

    PhotoWallCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:collectionCellIndentider forIndexPath:indexPath];

    cell.backgroundColor =[UIColor yellowColor];

    return cell;

    }

    - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

    {

    NSLog(@"你点击了");

    }

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

    return 6;

    }

    - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    return 3;

    }

    //设置session区头内容

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

    UICollectionReusableView *reusableview = nil;

    if (kind == UICollectionElementKindSectionHeader){

    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:collectionCellIndentider forIndexPath:indexPath];

    headerView.backgroundColor = [UIColor redColor];

    //把想添加的控件放在session区头重用的cell里,并且回来赋值,防止重用(重点!!!!!)

    UILabel *ttLabel = (UILabel *)[headerView viewWithTag:111];

    SelectTypeM*mm =_allDate[indexPath.section];

    ttLabel.text =mm.proportyName;

    reusableview = headerView;

    }

    return reusableview;

    }

    - (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

    }

    相关文章

      网友评论

        本文标题:ios的collectionView区头设置

        本文链接:https://www.haomeiwen.com/subject/yuxtettx.html