UICollectionViewController的簡(jiǎn)單使用

注冊(cè)CellId

static NSString *const XXCellId = @"XXCellId";

注冊(cè)Cell

[self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([XXCell class]) bundle:nil] forCellWithReuseIdentifier:XXCellId];

初始化&賦值

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

XXCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:XXCellId forIndexPath:indexPath];

cell.cellData = [[[self.XXModel.xxx objectAtIndex:indexPath.section] yyy] objectAtIndex:indexPath.item];

}

必須設(shè)置的布局

- (instancetype)init;

- (instancetype)init{

// 設(shè)置流水布局

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

layout.itemSize = CGSizeMake(100, 100);

// 設(shè)置最小行間距

layout.minimumLineSpacing = 2;

// 設(shè)置最小垂直間距

layout.minimumInteritemSpacing = 2;

// 設(shè)置滾動(dòng)方向(默認(rèn)垂直滾動(dòng))

layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

return [self initWithCollectionViewLayout:layout];

}

定義展示的Section的個(gè)數(shù)

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

return 2;

}

定義展示的UICollectionViewCell的個(gè)數(shù)

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

return 3;

}

定義每個(gè)UICollectionView 的大小

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

return CGSizeMake(80, 80);

}

定義每個(gè)UICollectionView 的 margin

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{

return UIEdgeInsetsMake(5, 10, 5, 10);

}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{

width 為水平滑動(dòng)時(shí),間距有效鲫构。height 為垂直滑動(dòng)時(shí),間距有效。

return CGSizeMake( width,height );

}

區(qū)頭 ? 與cell大概相同

static NSString *const HeaderViewId = @"HeaderViewId";

[self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([HeaderView class]) bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HeaderViewId];

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{

HeaderView *HeaderView ?= [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HeaderViewId forIndexPath:indexPath];

return HeaderView;

}

是否允許選中 默認(rèn)否

- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath{

return YES;

}

選中的狀態(tài)

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

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末裆泳,一起剝皮案震驚了整個(gè)濱河市酥艳,隨后出現(xiàn)的幾起案子景殷,更是在濱河造成了極大的恐慌,老刑警劉巖垦藏,帶你破解...
    沈念sama閱讀 216,402評(píng)論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異伞访,居然都是意外死亡掂骏,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門咐扭,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)芭挽,“玉大人滑废,你說(shuō)我怎么就攤上這事⊥嘧Γ” “怎么了蠕趁?”我有些...
    開封第一講書人閱讀 162,483評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)辛馆。 經(jīng)常有香客問(wèn)我俺陋,道長(zhǎng),這世上最難降的妖魔是什么昙篙? 我笑而不...
    開封第一講書人閱讀 58,165評(píng)論 1 292
  • 正文 為了忘掉前任腊状,我火速辦了婚禮,結(jié)果婚禮上苔可,老公的妹妹穿的比我還像新娘缴挖。我一直安慰自己,他們只是感情好焚辅,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,176評(píng)論 6 388
  • 文/花漫 我一把揭開白布映屋。 她就那樣靜靜地躺著,像睡著了一般同蜻。 火紅的嫁衣襯著肌膚如雪棚点。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,146評(píng)論 1 297
  • 那天湾蔓,我揣著相機(jī)與錄音瘫析,去河邊找鬼。 笑死默责,一個(gè)胖子當(dāng)著我的面吹牛贬循,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播傻丝,決...
    沈念sama閱讀 40,032評(píng)論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼甘有,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了葡缰?” 一聲冷哼從身側(cè)響起亏掀,我...
    開封第一講書人閱讀 38,896評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎泛释,沒想到半個(gè)月后滤愕,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,311評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡怜校,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,536評(píng)論 2 332
  • 正文 我和宋清朗相戀三年间影,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片茄茁。...
    茶點(diǎn)故事閱讀 39,696評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡魂贬,死狀恐怖巩割,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情付燥,我是刑警寧澤宣谈,帶...
    沈念sama閱讀 35,413評(píng)論 5 343
  • 正文 年R本政府宣布,位于F島的核電站键科,受9級(jí)特大地震影響闻丑,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜勋颖,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,008評(píng)論 3 325
  • 文/蒙蒙 一嗦嗡、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧饭玲,春花似錦侥祭、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至蚕断,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間入挣,已是汗流浹背亿乳。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留径筏,地道東北人葛假。 一個(gè)月前我還...
    沈念sama閱讀 47,698評(píng)論 2 368
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像滋恬,于是被迫代替她去往敵國(guó)和親聊训。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,592評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容