用xib創(chuàng)建uicollectionView

#import "AspectCell.h"

#import "AspectCollectionViewCell.h"

@interface AspectCell () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@end

@implementation AspectCell

- (void)awakeFromNib {

? ? [super awakeFromNib];

? ? self.backgroundColor = [UIColor whiteColor];


? ? UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];

? ? layout.minimumInteritemSpacing = 0;

? ? layout.minimumLineSpacing = 0;

? ? layout.itemSize=CGSizeMake(190,90);

? ? layout.sectionInset=UIEdgeInsetsMake(0,0,0,0);

? ? layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;//橫行滾動(dòng)

? ? _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 90) collectionViewLayout:layout];

? ? _collectionView.delegate = self;

? ? _collectionView.dataSource = self;

? ? _collectionView.showsVerticalScrollIndicator = NO;

? ? _collectionView.showsHorizontalScrollIndicator = NO;

? ? [_collectionView setBackgroundColor:[UIColor whiteColor]];


? ? //注冊(cè)cell

? ? [_collectionView registerNib:[UINib nibWithNibName:@"AspectCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"XibCell"];

//? ? [_collectionView registerClass:[AspectCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];

? ? [self.contentView addSubview:self.collectionView];


}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

? ? [supersetSelected:selectedanimated:animated];

? ? // Configure the view for the selected state

}

#pragma mark -- Collection delegate

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


? ? return self.collectDataArray.count;

}

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


? ? AspectCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: @"XibCell" forIndexPath:indexPath];

? ? AspectModel *model=self.collectDataArray[indexPath.row];


? ? cell.titleLab.text=model.title;

? ? cell.timeLab.text=model.time;

? ? cell.pageViewLab.text=model.pageviews;


//? ? cell.backgroundColor = [UIColor clearColor];

//? ? cell.textLabel.text = self.collectDataArray[indexPath.row];

//? ? cell.imageView.image = [UIImage imageNamed:@"MyTask@2x"];

? ? returncell;

}

#pragma mark -- Collection delegate

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

? ? NSLog(@"點(diǎn)擊了 %ld ", indexPath.row);


? ? if([self.delegaterespondsToSelector:@selector(CustomCollection:didSelectRowAtIndexPath:str:)]){

? ? ? ? [self.delegateCustomCollection:collectionViewdidSelectRowAtIndexPath:indexPathstr:self.collectDataArray[indexPath.row]];

? ? }



}

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

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

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