一礼预、dequeueReusableCellWithReuseIdentifier 走的是對應(yīng)的cell什么創(chuàng)建方法?
初步猜測是跟注冊方式有關(guān)
如果有關(guān)UINib * hotCityNib=[UINibnibWithNibName:@"HotCityCell"bundle:nil];
[self.collectionViewregisterNib:hotCityNib forCellWithReuseIdentifier:COLLECT_CELL_ID];
那么對應(yīng)的是是cell的awakefromXib的創(chuàng)建方法
如果有關(guān)[collection registerClass:[UICollectionCell class]forCellWithReuseIdentifier:cellIdentifier];
那么對應(yīng)的是cell的initwithframe的創(chuàng)建方法
二栏账、UICollectionView使用的cell重用機制
1.首先服從<UICollectionViewDataSource>協(xié)議,如果自定義cell,導(dǎo)入自定義cell類的頭文件
2.定義全局變量重用標(biāo)識符
static NSString *cellIdentifier = @“重用”;
3.注冊cell(collection,為UICollectionView對象)
[collection registerClass:[UICollectionCell class] forCellWithReuseIdentifier:cellIdentifier];
4.創(chuàng)建cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
//使用自定義cell,用自定義cell類將UICollectionCell替換即可
UICollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
return cell;
}
而tableviewcell 出了上面之外 有單獨的創(chuàng)建方法
(3).判斷是否成功取到可重用的cell,如果沒有創(chuàng)建一個cell
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier: identifier] ;
} return cell;}
三.兩種cell復(fù)用的方法區(qū)別:
假設(shè)我們已經(jīng)創(chuàng)建了一個collectionView,下面的1和2到底有什么區(qū)別呢
define COLLECT_CELL_ID @"collectionCellId"
1.[self.collectionView dequeueReusableCellWithReuseIdentifier:COLLECT_CELL_ID];
2.[self.collectionView dequeueReusableCellWithReuseIdentifier:COLLECT_HOTCITY_CELL_ID forIndexPath:indexPath];
對于這兩個方法,看了蘋果的官方文檔,給出的答案是這樣的(以XIB創(chuàng)建的cell為例)
對于第二種方式啥酱,必須用注冊的方法來實現(xiàn),即實現(xiàn)
UINib * hotCityNib=[UINibnibWithNibName:@"HotCityCell"bundle:nil];
[self.collectionViewregisterNib:hotCityNib forCellWithReuseIdentifier:COLLECT_CELL_ID];
否則會導(dǎo)致程序崩潰厨诸。
對于第一種方式镶殷,則使用普通方法和注冊方法都可以。
但是到了這里微酬,肯定還是有人會問绘趋,到底根本上的區(qū)別在哪里呢颤陶,經(jīng)過多方查閱以及自己進行實驗,終于發(fā)現(xiàn)了陷遮,原來使用第二種方法在出列cell之前會再次調(diào)用 sizeForItemAtIndexPath (tableView中為 heightForRowAtIndexPath)方法