码迷,mamicode.com
首页 > 其他好文 > 详细

UICollectionView

时间:2015-06-29 16:12:04      阅读:84      评论:0      收藏:0      [点我收藏+]

标签:

collectionView用法和我们常用的UITableview用法差不多,两个代理

<UICollectionViewDataSource,UICollectionViewDelegate>

下面就是关于UIcollectionView的使用

首先我们创建一个UICollectionViewCell,初始化

@implementation CollectionViewCell

-(instancetype)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        [self createViewCell];

    }

    return self;

}

 - (void)createViewCell

{

    self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];

    self.label.backgroundColor = [UIColor greenColor];

    [self addSubview:self.label];

}

@end

 然后再Viewcontroller中初始化UIcollectionView,添加代理方法

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

    view.itemSize = CGSizeMake(60, 60);

    //列编剧

    view.minimumInteritemSpacing = 30;

    //行间距

    view.minimumLineSpacing = 10;

    //header区大小

//    view.headerReferenceSize = CGSizeMake(100, 100);

    //底部区域大小

//    view.footerReferenceSize = CGSizeMake(100, 100);

    //滚动方向

    view.scrollDirection = UICollectionViewScrollDirectionVertical;

    //item与周围边缘的间距

    view.sectionInset = UIEdgeInsetsMake(20, 22, 20, 22);

    

    UICollectionView *cv = [[UICollectionView alloc] initWithFrame:[[UIScreen mainScreen] bounds] collectionViewLayout:view];

    [self.view addSubview:cv];

    cv.delegate = self;

    cv.dataSource = self;

    //注册cell

    [cv registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"cell"];

}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView

{

    return 100;

}

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

{

    return 20;

}

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

{

    static NSString *cellID = @"cell";

    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];

    cell.label.text = @"可一更牛逼";

    cell.label.numberOfLines = 0;

    return cell;

}

 

UICollectionView

标签:

原文地址:http://www.cnblogs.com/darain0212/p/4607453.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!