自定義UICollectionviewCell簡述

最近用到了UICollectionviewCell,系統(tǒng)的Cell不能滿足本身的需求,然后開始查找相關(guān)資料。UICollectionView是iOS6之后新引進(jìn)的API,用于展示集合視圖,布局更加靈活,其用法類似于UITableView。

使用UICollectionView需要實(shí)現(xiàn)UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout這三個(gè)協(xié)議,這是因?yàn)閁ICollectionViewDelegateFlowLayout實(shí)際上是UICollectionViewDelegate的一個(gè)子協(xié)議,它繼承了UICollectionViewDelegate,它的作用是提供一些定義UICollectionView布局模式的函數(shù)。

首先從創(chuàng)建collectionView開始,畢竟沒有視圖cell的效果也看不出來。

一、首先創(chuàng)建一個(gè)流水布局cell,設(shè)置cell的一些屬性

//創(chuàng)建一個(gè)流水布局
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
//設(shè)置cell的尺寸(寬度和高度)
layout.itemSize = CGSizeMake((RBScreenW-2)/2.0, 250);
//設(shè)置豎直滾動放向(默認(rèn)是豎直方向)
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
//設(shè)置cell與cell之間的列距
layout.minimumInteritemSpacing = 1;
//設(shè)置cell與cell之間的行距
layout.minimumLineSpacing = 1;

注意:根據(jù)自己的需求,自行設(shè)置。還有其他屬性,自己到頭文件查看唄!O(∩_∩)O哈哈~

二、初始化collectionView

//創(chuàng)建UICollectionView
UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
collectionView.backgroundColor = [UIColor whiteColor];
//添加到視圖
[self.view addSubview:collectionView];
collectionView.scrollsToTop = NO;
//開啟分頁
collectionView.pagingEnabled = YES;
//不顯示滾動條
collectionView.showsHorizontalScrollIndicator = NO;
//彈簧效果設(shè)置
collectionView.bounces = NO;
//設(shè)置代理
collectionView.dataSource = self;
collectionView.delegate = self;
//注冊cell
[collectionView registerClass:[RBAttendCell class] forCellWithReuseIdentifier:ID];

注意:這里沒有使用xib創(chuàng)建cell,需要使用registerClass: forCellWithReuseIdentifier:方法來注冊cell。

三、遵守協(xié)議(上面講了什么協(xié)議),實(shí)現(xiàn)代理方法。

//設(shè)置分組數(shù)
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
//設(shè)置每個(gè)分組個(gè)數(shù)
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}
//只有新的cell出現(xiàn)的時(shí)候才會調(diào)用
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
   
}

當(dāng)然,在此之前,需要自行創(chuàng)建一個(gè)CollectionViewCell繼承自UICollectionViewCell。
UICollectionViewCell不需要像UITableViewCell那樣判斷Cell是否已被創(chuàng)建,這是因?yàn)樵谧远xUICollectionViewCell時(shí)有一個(gè)極其重要的方法:

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        //添加自己需要個(gè)子視圖控件
        [self setUpAllChildView];
    }
    return self;
}

最后在數(shù)據(jù)源方法中添加cell就可以了。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    //只要有新的cell出現(xiàn),就把對應(yīng)的數(shù)據(jù)模型添加到新的cell中
    RBAttendCell *cell = (RBAttendCell *)[collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    //移除之前的子控制器的view
    //[cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    RBLiveModel *item = self.dataArray[indexPath.row];
    cell.liveItem = item;
    return cell;
}

這里使用的數(shù)據(jù)模型自己設(shè)置,或者網(wǎng)絡(luò)上獲取比如微博的數(shù)據(jù),直播的數(shù)據(jù),解析出部分?jǐn)?shù)據(jù)轉(zhuǎn)變成數(shù)據(jù)模型。顯示到collectionView上。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容