拖拉控件collectionView的使用

关系如下,和tableView一样:

 

要在自定义的cell里面添加如下代码:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        // 初始化时加载collectionCell.xib文件
        NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"CollectionViewCell" owner:self options:nil];
        
        // 如果路径不存在,return nil
        if (arrayOfViews.count < 1)
        {
            return nil;
        }
        // 如果xib中view不属于UICollectionViewCell类,return nil
        if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]])
        {
            return nil;
        }
        // 加载nib
        self = [arrayOfViews objectAtIndex:0];
    }
    return self;
}
再实现它的delegate和datasource

还有

self.collectionView.backgroundColor = [UIColor whiteColor];

[self.collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"collectionCellReuse"];

posted @ 2015-12-11 09:31  宋婷婷  阅读(200)  评论(0编辑  收藏  举报