//pragma mark - 數(shù)據(jù)源
// 返回有多少組"如果不實(shí)現(xiàn)此方法默認(rèn)就是1組"
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 2;
}
// 返回每一組有多少個(gè)格子
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 100;
}
// 返回每一組的每一個(gè)格子
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
//用dequeueReusableCellWithReuseIdentifier:forIndexPath:此方法來獲取cell之前必須要提前注冊cell"告訴collectionView它所需要的cell該如果去創(chuàng)建"
// UICollectionView中的cell只能用dequeue的方法來獲取或創(chuàng)建
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
return cell;
}
/**
* cell有三種注冊方式:
1.xib方式注冊cell,并設(shè)置重用標(biāo)識"如果需要?jiǎng)?chuàng)建cell就會(huì)自動(dòng)加載注冊時(shí)指定的xib文件來創(chuàng)建cell"
2.class方式注冊cell,并且設(shè)置重用標(biāo)識"如果需要?jiǎng)?chuàng)建cell就會(huì)自動(dòng)調(diào)用注冊此指定class的初始化方法來創(chuàng)建cell"
3.storyboard方式注冊cell"只需要給storyboard中的cell設(shè)置重用標(biāo)識就可以完成注冊,如果需要?jiǎng)?chuàng)建cell會(huì)自動(dòng)的加載storyboard中的cell來創(chuàng)建"
*/
/**
* xib方式注冊cell
*/
{
// 設(shè)置cell的尺寸
// self.flowLayout.itemSize = CGSizeMake(100, 120);
// 加載xib文件
UINib *nib = [UINib nibWithNibName:@"HMAppCell" bundle:nil];
HMAppCell *cell = [[nib instantiateWithOwner:nil options:nil] lastObject];
self.flowLayout.itemSize = cell.bounds.size;
// 通過xib文件來注冊cell"告訴collectionView它所需要的cell該如何去創(chuàng)建"
// 用xib來注冊cell時(shí)xib文件中的cell最好不要設(shè)置重用標(biāo)識了,可能會(huì)出問題
[self.collectionView registerNib:nib forCellWithReuseIdentifier:ID];
}
/**
* class方式注冊cell
*/
/**
(報(bào)錯(cuò)內(nèi)容) UICollectionView must be initialized with a non-nil layout parameter
在創(chuàng)建UICollectionView時(shí)不能傳一個(gè)空的布局對象"必須傳一個(gè)布局對象"
*/
創(chuàng)建布局對象"通過UICollectionViewLayout創(chuàng)建出來的布局對象它里面沒有控件cell的大小及間距這些屬性,所以它是一個(gè)空的布局對象" 所以不能直接用alloc init直接創(chuàng)建
// 創(chuàng)建一個(gè)流水布局,它里面有用來控件cell大小及間距的屬性
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
// 設(shè)置cell的尺寸
flowLayout.itemSize = CGSizeMake(100, 120);
// 1.創(chuàng)建UICollectionView
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
[self.view addSubview:collectionView];
//設(shè)置數(shù)據(jù)源
collectionView.dataSource = self;
// 通過class來注冊cell"告訴collectionView它將來自動(dòng)創(chuàng)建cell時(shí)該如何去創(chuàng)建"
// 將來它要?jiǎng)?chuàng)建cell時(shí)會(huì)調(diào)用此類的initWithFrame:
[collectionView registerClass:[HMAppCell class] forCellWithReuseIdentifier:ID];
"注: 創(chuàng)建時(shí)會(huì)調(diào)用HMAppCell類中的initWithFrame:方法,所以需要重寫initWithFrame:初始化方法在此方法中添加所有用來顯示數(shù)據(jù)的子控件;---要記得調(diào)用父類的initWithFrame:--- self = [super initWithFrame:frame];
"在該類中重寫- (void)layoutSubviews 方法調(diào)整子控件的frame;也要記得調(diào)用父類方法:[super layoutSubviews];
/**
* collectionView的屬性
*/
// 設(shè)置collectionView的背景顏色
self.collectionView.backgroundColor = [UIColor whiteColor];
// 獲取collectionView中的布局屬性
UICollectionViewLayout *layout = self.collectionView.collectionViewLayout;
// 通過此屬性可以重新給collectionView設(shè)置一個(gè)新的布局對象"會(huì)覆蓋原本自帶的流布局"
// self.collectionView.collectionViewLayout = [[UICollectionViewFlowLayout alloc] init];
// 設(shè)置背景圖片,相當(dāng)于給collectionView添加了一個(gè)子控件
self.collectionView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"7"]];
// 選中cell
self.collectionView.allowsSelection = NO;
// 設(shè)置是否允許多選
self.collectionView.allowsMultipleSelection = YES;
/************ 以上是UICollectionView的常用屬性 ************/
// 通過控制器中去獲取布局對象
// 通過控制器的此屬性只能獲取布局對象,不能更換布局對象
UICollectionViewFlowLayout *layout1 = (UICollectionViewFlowLayout *)self.collectionViewLayout;
layout1.itemSize = CGSizeMake(100, 100);
/******** 以上是UICollectionViewController的屬性********/
/***********以下是流布局對象中的屬性***((***********/
// 設(shè)置cell的尺寸
// self.flowLayout.itemSize = CGSizeMake(100, 100);
// 設(shè)置滾動(dòng)方向
// 當(dāng)滾動(dòng)方向發(fā)生顛倒后,最小行間距和列間距也會(huì)相應(yīng)的顛倒,頭部和尾部的寬或高也會(huì)發(fā)生相應(yīng)顛倒
self.flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
// 設(shè)置最小行間距
self.flowLayout.minimumLineSpacing = 10;
// 設(shè)置最小列間距
self.flowLayout.minimumInteritemSpacing = 20;
// 設(shè)置頭部視圖的尺寸"垂直滾動(dòng)時(shí),頭部的寬是改不了得,如果是水平方向滾動(dòng),頭部的高是不能改得"
self.flowLayout.headerReferenceSize = CGSizeMake(20, 100);
// 設(shè)置尾部視圖的尺寸"垂直滾動(dòng)時(shí),尾部的寬是改不了得,如果是水平方向滾動(dòng),尾部的高是不能改得"
self.flowLayout.footerReferenceSize = CGSizeMake(20, 100);
// 設(shè)置組的內(nèi)邊距
self.flowLayout.sectionInset = UIEdgeInsetsMake(20, 40, 70, 100);
// 當(dāng)前組還在可視范圍內(nèi)時(shí)頭部是否停留
self.flowLayout.sectionHeadersPinToVisibleBounds = YES;
// 當(dāng)前組還在可視范圍內(nèi)時(shí)尾部是否停留
self.flowLayout.sectionFootersPinToVisibleBounds = YES;
// 設(shè)置cell的預(yù)估尺寸"如果設(shè)置了預(yù)估尺寸一定要在cell中實(shí)現(xiàn)下面方法返回cell真是尺寸:
- (UICollectionViewLayoutAttributes *)preferredLayoutAttributesFittingAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
// 此方法要結(jié)合預(yù)估尺寸屬性來使用,如果沒有設(shè)置預(yù)估尺寸,此方法也會(huì)調(diào)用,只不過,它里面的設(shè)置是無效
//實(shí)現(xiàn)此方法還必須要調(diào)用父類此方法
//預(yù)估尺寸
self.flowLayout.estimatedItemSize = CGSizeMake(100, 100);
// 返回每一組的頭部或尾部視圖
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
// 1.定義重用標(biāo)識
static NSString *ID = @"header";
if (kind == UICollectionElementKindSectionFooter) {
ID = @"footer";
} else {
ID = @"header";
}
//NSLog(@"%@", kind);
// 獲取每一組的頭部或尾部視圖
UICollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:ID forIndexPath:indexPath];
return reusableView;
}
// 代理方法
// 取消上一次選中的某個(gè)cell
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
//NSLog(@"取消上一次選中的第%zdcell", indexPath.item);
}
// 選中某一個(gè)cell
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
// NSLog(@"選中第%zd個(gè)cell", indexPath.item);
}
// 流布局中的代理方法
// 返回每一個(gè)格子的具體尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.item % 2 == 0) {
return CGSizeMake(20, 80);
} else {
return CGSizeMake(40, 40);
}
}
"注:
/**
the item width must be less than the width of the UICollectionView minus the section insets left and right values, minus the content insets left and right values.
當(dāng)collectionView是垂直方向滾動(dòng)時(shí),cell的寬必須要小于 collectionView的寬 - 組的左邊邊距 - 組的右邊邊距
當(dāng)collectionView是水平方向滾動(dòng)時(shí),cell的高必須小于 collectionView的高 - 組的頂部邊距 - 組的底部邊距
// cell尺寸也不能小于或等于 CGSizeZero
*/
UICollectionView的使用
最后編輯于 :
?著作權(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ā)布平臺,僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲(chǔ)服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- UICollectionView的使用方法及demo 直接上代碼,說明請看注釋吧ViewController.h ...
- 很多項(xiàng)目中用都會(huì)有相冊功能,實(shí)現(xiàn)相冊中瀏覽的圖片的功能,你可能會(huì)想到使用 scrollview ,但我更建議你通過...
- UICollectionview的基本使用 一些小心得 UICollectionview 其實(shí)和UITablevi...
- 1.分析界面由哪些控件組成? 界面有兩行,可以用tableView的靜態(tài)cell來搭建注意:靜態(tài)cell只能通過s...
- 傳送門: UICollectionView官方文檔 UICollectionView的簡單介紹 因?yàn)閁IColle...