UICollection布局

来源:互联网 发布:c语言 sleep 1 编辑:程序博客网 时间:2024/05/20 18:17

       UICollectionView 是针对IOS6 以后才能使用的 控件,比起UITableView来说功能更强大,使用起来更方便!

使用UICollectionView最重要的一点就是加载设置UICollectionViewFlowLayout,一下是自己总结的使用UICollectionView的具体使用步骤:


一:创建 UICollectionView控件 并 registerClass 具体代如下:

SQCollectionView = [[UICollectionViewalloc]initWithFrame:CGRectMake(0,64,SCREEN_WIDTH,SCREEN_HEIGHT-64)collectionViewLayout:[[collectionViewFlowLayoutalloc]init]];

    [SQCollectionViewregisterClass:[CollectionViewCellclass]forCellWithReuseIdentifier:@"cell"];

    SQCollectionView.delegate =self;

    SQCollectionView.dataSource =self;

    SQCollectionView.contentOffset =CGPointMake(0,0);

    SQCollectionView.backgroundColor=[UIColorclearColor];

    [SQCollectionViewsetContentInset:UIEdgeInsetsMake(0,0,0,0)];

    [self.viewaddSubview:SQCollectionView];


二:创建UICollectionViewCell 用于每个Item 中具体UI控件的填充类似于UITableViewCell 具体代码根据自己功能需求来定:

 self.coverImageView = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0,collectionview_cell_width,125)];

    [self.coverImageView.layersetCornerRadius:5];

    self.coverImageView.layer.masksToBounds = YES;

    [self.contentViewaddSubview:self.coverImageView];


     self.nameLabel = [[UILabelalloc]initWithFrame:CGRectMake(0,132,collectionview_cell_width,15)];

    self.nameLabel.font = [UIFontsystemFontOfSize:15];

    [self.contentViewaddSubview:self.nameLabel];


三:创建UICollectionViewFlowLayout 设置其布局,具体设置代码如下:

self = [superinit];

    if (self) {

        self.itemSize =CGSizeMake(ITEM_WIDTH,ITEM_HEIGHT);

        //排列方向 (这里需要注意的是 可以设置横向滑动 对一些特定功能来说更方便处理)

        self.scrollDirection =UICollectionViewScrollDirectionVertical;

        self.sectionInset =UIEdgeInsetsMake(8,10,15,10);

        //行间隔

        self.minimumLineSpacing =15;

        self.minimumInteritemSpacing =7;

    }

    returnself;

设置完这些之后 剩下就是 UICollectionViewDelegate 的一些代理方法了

#pragma mark - collectionViewDelegate

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

{

    return20;

}


-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView

{

    return1;

}


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

{

    staticNSString * cellIndetifier =@"cell";

    CollectionViewCell * cell = [collectionViewdequeueReusableCellWithReuseIdentifier:cellIndetifierforIndexPath:indexPath];

    [cell loadCollectionUI:[NSStringstringWithFormat:@"%ld个元素",(long)indexPath.row]];

    return cell;

}

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

{


}



0 0
原创粉丝点击