iOS UITableViewCell中包含UICollectionView

最近公司需要修改主界面捌斧,把以前所有服務(wù)類型都統(tǒng)一類型顯示改為分模塊化顯示反镇。也就造成以前用一個collectionView解決的事情,需要使用到UITableViewCell給不同類型的服務(wù)分組。每一個cell中的服務(wù)個數(shù)又不確定爵嗅,只能TableViewCell種嵌套UICollectionView趋艘。

步驟 1. 使用xib來定義一個cell疲恢,用UICollectionView來填充這個cell, 使用上下左右約束來根據(jù)不同高度的collectionView 撐起這個cell.


屏幕快照 2018-06-03 下午3.32.36.png

步驟2. 在xib中設(shè)置UICollection的屬性,如 scroll Direction 設(shè)置為Vertical 垂直顯示瓷胧。 Scrolling Enable設(shè)置為NO显拳。


屏幕快照 2018-06-03 下午3.33.17.png

在代碼中設(shè)置屬性

 UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.minimumInteritemSpacing = 2.0;
    flowLayout.minimumLineSpacing = 1.0f;
//    flowLayout.itemSize = CGSizeMake(DEVICEWIDTH/4.0-0, DEVICEWIDTH/4.0-8);
//    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    
    //設(shè)置代理
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
//    self.collectionView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerClass:[NTNewOfficeServicCollectionCell class] forCellWithReuseIdentifier:@"collectionCell"];
    
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView"];

步驟3. 在tableViewCell中設(shè)置collectionView 的 dataSource Delegate等。

#pragma mark -- UICollectionDataSource
#pragma mark -- UICollectionViewDataSource
//定義展示的UICollectionViewCell的個數(shù)
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.serviceListArr.count;
}

//定義展示的Section的個數(shù)
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}


//???
//每個UICollectionView展示的內(nèi)容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NTNewOfficeServicCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCell" forIndexPath:indexPath];
    [cell sizeToFit];
    NTSubscribeModel *model = self.serviceListArr[indexPath.row];
    NSString *serViceIconName = [model.iconIOS lastPathComponent];
    NSString *imageName = [[serViceIconName componentsSeparatedByString:@"@3x"] firstObject];
    [cell.imageView downloadImageBycontentUrlStr:model.iconIOS andIsCut:NO placeholdImage:[UIImage imageNamed:[NSString stringWithFormat:@"newOffice_%@",imageName]] options:SDWebImageRefreshCached];
    //SW:  cacheType = SDImageCacheTypeMemory && SDWebImageRefreshCached   內(nèi)存緩存 && 重新下載刷新緩存搓萧, 頻繁reloadData 將導(dǎo)致內(nèi)存持續(xù)增加
    cell.textName.text = model.serviceName;
    model = nil;
    return cell;
}


//頭部展示的內(nèi)容
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView" forIndexPath:indexPath];
    
    return headView;
}

//定義每個UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((DEVICEWIDTH-20-15)/4.0f, DEVICEWIDTH/4.0f-5);
}

//定義每個UICollectionView 的間距
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(5, 10, 5, 10);
}

//每個item之間的間距
- (CGFloat)collectionView:(UICollectionView *)collectionView
                   layout:(UICollectionViewLayout*)collectionViewLayout
minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 2;
}


////動態(tài)設(shè)置每列的間距大小
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
//    return 1.0f;
//}

//???
//UICollectionView被選中時調(diào)用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NTSubscribeModel *model = self.serviceListArr[indexPath.row];
    if(self.newOfficeServiceWasSelect){
        self.newOfficeServiceWasSelect(model, indexPath.row, self);
    }
}

步驟4. 當(dāng)在tableView中獲取到數(shù)據(jù)時杂数,刷新UitableView , 把需要顯示的數(shù)據(jù)賦值給tableViewCell, 在使用collectionView realoadData, 刷新collectView.

NTNewOfficeServiceCell *cell = [tableView dequeueReusableCellWithIdentifier:NEWIMPORTSERVICECELL forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        //下面這兩個語句一定要添加,否則第一屏顯示的collection view尺寸瘸洛,以及里面的單元格位置會不正確
        cell.frame = tableView.bounds;
        [cell layoutIfNeeded];
        [cell reloadDataWithServiceList:(indexPath.row == 1)?[[self.moduleListDic objectForKey:@"base"] copy]:[[self.moduleListDic objectForKey:@"other"] copy]];
        cell.isShowTitleView = (indexPath.row == 1)?NO:YES;
        
        NTWeakself;
        [cell setNewOfficeServiceWasSelect:^(NTSubscribeModel *subscribModel, NSInteger index, NTNewOfficeServiceCell *unReadCell) {
            NTStrongself;
            [strongself configServiceWasSelect:unReadCell selectIndex:index selectModel:subscribModel];
        }];
        return cell;

遇到的問題揍移。

  1. 刷新tableView時。使用setter方法中刷新collctionView的高度時反肋,UITableViewCell高度不實(shí)時更新那伐。

解釋: 剛開始是collectionView中賦值時,使用的是直接賦值屬性石蔗,也就是‘cell.arr = [arr copy]’, 在collection中使用setArr中來賦值罕邀,并且刷新collection。本來一行可以顯示四個养距,當(dāng)賦值四個item的時候燃少,collectionView的contentSize高度會變成兩行的高度。導(dǎo)致下一行不顯示數(shù)據(jù)铃在,就是撐過了阵具。
查了一下代碼,使用方法賦值后再刷新定铜,就不會出現(xiàn)這個問題阳液。

- (void)reloadDataWithServiceList:(NSArray *)servicetArr {
    if(servicetArr){
        _serviceListArr = servicetArr;
        [self.collectionView reloadData];
      
        //計算collectionView的contentSize的高度,再從新更新collectView的高度約束
        CGFloat height = self.collectionView.collectionViewLayout.collectionViewContentSize.height;
        self.collectionViewConstraint.constant = height;
        [self.collectionView.collectionViewLayout invalidateLayout];
    }
}
  1. 解決1問題后揣炕,首次刷新時帘皿,UICollectionViewCell的寬高度設(shè)置來撐起CollectView高度的預(yù)期值不一樣。造成有一個動畫顯示畸陡。

解決方法:在tableViewCell中給UICollectionViewCell賦值時鹰溜,在前面添加下面兩行代碼就OK虽填,提前刷新一下tableViewCell.

//下面這兩個語句一定要添加,否則第一屏顯示的collection view尺寸曹动,以及里面的單元格位置會不正確
        cell.frame = tableView.bounds;
        [cell layoutIfNeeded];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末斋日,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子墓陈,更是在濱河造成了極大的恐慌恶守,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,525評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件贡必,死亡現(xiàn)場離奇詭異兔港,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)仔拟,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,203評論 3 395
  • 文/潘曉璐 我一進(jìn)店門衫樊,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人利花,你說我怎么就攤上這事科侈。” “怎么了晋被?”我有些...
    開封第一講書人閱讀 164,862評論 0 354
  • 文/不壞的土叔 我叫張陵兑徘,是天一觀的道長。 經(jīng)常有香客問我羡洛,道長挂脑,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,728評論 1 294
  • 正文 為了忘掉前任欲侮,我火速辦了婚禮崭闲,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘威蕉。我一直安慰自己刁俭,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,743評論 6 392
  • 文/花漫 我一把揭開白布韧涨。 她就那樣靜靜地躺著牍戚,像睡著了一般。 火紅的嫁衣襯著肌膚如雪虑粥。 梳的紋絲不亂的頭發(fā)上如孝,一...
    開封第一講書人閱讀 51,590評論 1 305
  • 那天,我揣著相機(jī)與錄音娩贷,去河邊找鬼第晰。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的茁瘦。 我是一名探鬼主播品抽,決...
    沈念sama閱讀 40,330評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼甜熔!你這毒婦竟也來了圆恤?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,244評論 0 276
  • 序言:老撾萬榮一對情侶失蹤纺非,失蹤者是張志新(化名)和其女友劉穎哑了,沒想到半個月后赘方,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體烧颖,經(jīng)...
    沈念sama閱讀 45,693評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,885評論 3 336
  • 正文 我和宋清朗相戀三年窄陡,在試婚紗的時候發(fā)現(xiàn)自己被綠了炕淮。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,001評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡跳夭,死狀恐怖涂圆,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情币叹,我是刑警寧澤润歉,帶...
    沈念sama閱讀 35,723評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站颈抚,受9級特大地震影響踩衩,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜贩汉,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,343評論 3 330
  • 文/蒙蒙 一驱富、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧匹舞,春花似錦褐鸥、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,919評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至姊舵,卻和暖如春晰绎,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背蠢莺。 一陣腳步聲響...
    開封第一講書人閱讀 33,042評論 1 270
  • 我被黑心中介騙來泰國打工寒匙, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 48,191評論 3 370
  • 正文 我出身青樓锄弱,卻偏偏與公主長得像考蕾,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子会宪,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,955評論 2 355

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