UICollectionView的使用方法

来源:互联网 发布:广元外卖软件 编辑:程序博客网 时间:2024/06/08 14:25

- (void)viewDidLoad {

    [superviewDidLoad];

    self.view.backgroundColor = [UIColorlightGrayColor];

    //确定是水平滚动,还是垂直滚动

    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayoutalloc] init];

    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];

    

    _collectionView=[[UICollectionViewalloc] initWithFrame:CGRectMake(0,64, self.view.frame.size.width,self.view.frame.size.height-64)collectionViewLayout:flowLayout];

    _collectionView.dataSource=self;

    _collectionView.delegate=self;

    [_collectionViewsetBackgroundColor:[UIColorgreenColor]];

    

    //注册Cell,必须要有

    [_collectionViewregisterClass:[UICollectionViewCellclass] forCellWithReuseIdentifier:@"UICollectionViewCell"];

    

    [self.viewaddSubview:_collectionView];


}

#pragma mark - collectionView dataSource Or delegate

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

{

    return50;

}

//每个UICollectionView展示的内容

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

{

    staticNSString * CellIdentifier = @"UICollectionViewCell";

    UICollectionViewCell * cell = [collectionViewdequeueReusableCellWithReuseIdentifier:CellIdentifierforIndexPath:indexPath];

    

    cell.backgroundColor = [UIColorcolorWithRed:((10 * indexPath.row) /255.0) green:((20 * indexPath.row)/255.0)blue:((30 * indexPath.row)/255.0)alpha:1.0f];

    UILabel *label = [[UILabelalloc] initWithFrame:CGRectMake(0,0, 20,20)];

    label.textColor = [UIColorredColor];

    label.text = [NSStringstringWithFormat:@"%ld",(long)indexPath.row];

    

    for (id subViewin cell.contentView.subviews) {

        [subView removeFromSuperview];

    }

    [cell.contentViewaddSubview:label];

    return cell;

}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView

{

    return1;

}

//定义每个UICollectionView margin

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

{

    returnUIEdgeInsetsMake(5,5, 5,5);

}

//UICollectionView被选中时调用的方法

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

{

    UICollectionViewCell * cell = (UICollectionViewCell *)[collectionViewcellForItemAtIndexPath:indexPath];

    //临时改变个颜色,看好,只是临时改变的。如果要永久改变,可以先改数据源,然后在cellForItemAtIndexPath中控制。(和UITableView差不多吧!O(∩_∩)O~

    cell.backgroundColor = [UIColorgreenColor];

    NSLog(@"item======%ld",(long)indexPath.item);

    NSLog(@"row=======%ld",(long)indexPath.row);

    NSLog(@"section===%ld",(long)indexPath.section);

}

//定义每个Item的大小

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

{

    returnCGSizeMake(100,100);

}

//返回这个UICollectionView是否可以被选择

-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath

{

    returnYES;

}


- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

0 0
原创粉丝点击