多樣式cell的重用機(jī)制
學(xué)習(xí)IOS已經(jīng)有差不多三個(gè)月時(shí)間了备燃,第一篇文章晌杰,記錄一下這幾個(gè)月來(lái)我以及周圍朋友初級(jí)階段都會(huì)遇到的一個(gè)問(wèn)題-----類似于圖上的功能(一個(gè)UITableView或者UICollectionView 通過(guò)選擇按鈕切換不同的數(shù)據(jù)源、以及內(nèi)容)
這樣的構(gòu)造在許多應(yīng)用都會(huì)用到鲤孵,因此吧凉,我覺(jué)得這一個(gè)知識(shí)點(diǎn)很有必要掌握舍沙。很神奇的是,和我一起學(xué)習(xí)的童鞋們不會(huì)用的居然是占大多數(shù)颊亮。下面柴梆,我會(huì)大概來(lái)講一講它的使用步驟。(我的第一個(gè)APP 作為我的第一個(gè)demo终惑,有些小緊張/(ㄒoㄒ)/~~屏幕沒(méi)有適配绍在、233)
1.寫UITableView/UICollectionView
if(!_conllertionView){
_conllertionView = [UICollectionView alloc]initWithFrame(0,self.frame.size.height/3.0,
self.frame.size.width,self.frame.size.height - self.frame.size.height/3.0)];
_conllertionView.delegata = self;
_conllertionView.dataSource = self;
}
return _conllertionView;
注意---
代理寫完后,記住加上協(xié)議
UITableView:UITableViewDelegate / UITableViewDataSource
UICollectionView:UICollectionViewDelegate / UICollectionViewDataSource
隨后加上他們必須要的方法,這里我以UICollectionView為例
//Section個(gè)數(shù)
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
}
//數(shù)據(jù)個(gè)數(shù)(cell個(gè)數(shù))
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
}
//cell內(nèi)容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
}
//上下左右距離
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
}
//每個(gè)UICollectionView大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
}
//設(shè)置cell行距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
}
基本的設(shè)置上大概完成了偿渡、以下內(nèi)容是重點(diǎn)臼寄。首先分析我們點(diǎn)擊的每一個(gè)按鈕都會(huì)對(duì)應(yīng)不同的界面。
然后适揉,通過(guò)對(duì)界面的分析留攒,我們可以知道,完成這樣的效果是需要用到重用機(jī)制的嫉嘀。因此炼邀、我們需要用到不同的cell。所以剪侮,分別建立不同的基于(UICollectionViewCell的類拭宁,分別對(duì)其進(jìn)行設(shè)置。(這里注意把數(shù)據(jù)需要使用的控件定義為屬性放到.h文件里哦~)
做到這里大部分的工作都已經(jīng)完成了票彪,最復(fù)雜,同時(shí)也是許多新手不容易想到的地方出現(xiàn)了不狮。我們很容易想到這個(gè)效果會(huì)用不同的Cell,但是究竟如何使用卻成為了一個(gè)難題降铸。大多數(shù)新手會(huì)忘記“重用”,這里的重用不是簡(jiǎn)單的對(duì)相同樣式進(jìn)行重用摇零,而是利用它的重用機(jī)制推掸,完成不同樣式的切換效果。
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
if ([self.title1 isEqualToString:@"漫畫(huà)"]) {
[_conllertionView registerClass:[CartCollectionViewCell class] forCellWithReuseIdentifier:@"CartCell"];
CartCollectionViewCell *myCell=[collectionView dequeueReusableCellWithReuseIdentifier: @"CartCell" forIndexPath:indexPath];
//相關(guān)設(shè)置
self.nowCell = myCell;
}else if ([self.title1 isEqualToString:@"我的漫推"]){
[_conllertionView registerClass:[ManTuiCollectionViewCell class]forCellWithReuseIdentifier:@"MantuiCell"];
ManTuiCollectionViewCell *myCell1=[collectionView dequeueReusableCellWithReuseIdentifier: @"MantuiCell" forIndexPath:indexPath];
//相關(guān)設(shè)置
self.nowCell = myCell1;
}else if([self.title1 isEqualToString:@"動(dòng)畫(huà)"]){
[_conllertionView registerClass:[AnimaCollectionViewCell class] forCellWithReuseIdentifier:@"AnimaCell"];
AnimaCollectionViewCell *myCell2=[collectionView dequeueReusableCellWithReuseIdentifier:@"AnimaCell" forIndexPath:indexPath];
//相關(guān)設(shè)置
self.nowCell = myCell2; }
return self.nowCell;
}
我們利用它的重用機(jī)制驻仅,針對(duì)三次點(diǎn)擊谅畅,注冊(cè)了不同的cell,這樣噪服,就可以有我們想要達(dá)到的效果了毡泻。只有我們點(diǎn)擊了不同的標(biāo)簽,程序就會(huì)找到自己所對(duì)應(yīng)的cell進(jìn)行設(shè)置粘优,這步完成后仇味,千萬(wàn)不要忘記每個(gè)UICollectionView的其他屬性也會(huì)根據(jù)不同的標(biāo)簽進(jìn)行改變的。
(這里我以返回?cái)?shù)量為例雹顺,其他屬性設(shè)置方法都是雷同的)
//數(shù)據(jù)個(gè)數(shù)(cell個(gè)數(shù))
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
if ([self.title1 isEqualToString:@"漫畫(huà)"]) {
return "漫畫(huà)數(shù)據(jù)源個(gè)數(shù)";
}else if ([self.title1 isEqualToString:@"我的漫推"]){
return "我的漫推數(shù)據(jù)源個(gè)數(shù)";
}else if([self.title1 isEqualToString:@"動(dòng)畫(huà)"]){
return "漫畫(huà)數(shù)據(jù)源個(gè)數(shù)";
} else{
return 10;
}
}
總結(jié)
1.創(chuàng)建不同數(shù)據(jù)來(lái)存放數(shù)據(jù)(這里我推薦使用MVC模式丹墨,將數(shù)據(jù)放進(jìn)Mode里某些方面還能避免數(shù)據(jù)重用)
2.創(chuàng)建不同Cell使用其樣式
3.數(shù)據(jù)源改變,UICollectionView的其他屬性也會(huì)根據(jù)需求進(jìn)行改變
寫到這里嬉愧,大部分的東西也都講完了贩挣,如果還有什么地方不明白,可以一起進(jìn)行交流,探討王财。