UITableView里面嵌套collectionView(摘抄筆記)

在開發(fā)中總有這么一些場景,每個cell上有不定個按鈕或者View.怎嘛寫好呢?for循環(huán)布局?一個cell上如果很多view呢?......
把collectionView放在cell里其實(shí)挺好的,還有每個每個cell的點(diǎn)擊事件直接可以寫在 代理方法中.

效果圖

先簡要說明一下思路
1創(chuàng)建一個tableView加到控制器上 遵守協(xié)議實(shí)現(xiàn)代理方法...不拉不拉那一堆事...... 返回一個 固定高度(因?yàn)檫@個時候不知道那個每個cell的高度是多少合適,先固定 以后變)
2重寫tableViewCell 在cell中創(chuàng)建 collectionView并且加到cell中 注意 因?yàn)閏ollectionView的高度是根據(jù)cell來說的,這個時候還不知道cell有多少先隨便給一個定值 (后期再改)collectionView用masonry寫約束 是 top left right height
問題
怎么得到collectionView的內(nèi)容視圖的的高度呢? 知道collectionView的內(nèi)容視圖的的高度之后改了collectionView的高度,怎嘛改變 tableViewCell的高度呢?
一切盡在代碼中

控制器.h

//
//  ViewController.h
//  tableViewCell中加載collectionView
//
//  Created by 彭成 on 2018/12/27.
//  Copyright ? 2018年 SJJ. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@end

控制器.m

//
//  ViewController.m
//  tableViewCell中加載collectionView
//
//  Created by 彭成 on 2018/12/27.
//  Copyright ? 2018年 SJJ. All rights reserved.
//

#import "ViewController.h"
#import "testTableViewCell.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,testTableViewCellDelegate>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)testTableViewCell *toolCell;
@property(nonatomic,strong)NSArray *dataArr;
@property(nonatomic,strong)NSMutableDictionary *dicH;


@property(nonatomic,strong)UILabel *numLabel;
@property(nonatomic,strong)CADisplayLink *displayLink;
@property(nonatomic,assign)NSTimeInterval lastTime;
@property(nonatomic,assign)NSInteger   count;
@end
@implementation ViewController

-(NSArray *)dataArr{
    if (!_dataArr) {
        _dataArr = @[
                     @[@"g"],
                     @[@"g",@"b"],
                     @[@"g",@"b",@"a"],
                     @[@"g",@"b",@"c",@"e"],
                     @[@"g",@"b",@"c",@"e",@"a"],
                     @[@"g",@"b",@"c",@"e",@"b",@"c"],
                     @[@"g",@"b",@"c",@"e",@"b",@"c",@"a"],
                     @[@"g",@"b",@"c",@"e",@"b",@"c",@"b",@"c"],
                     @[@"g",@"b",@"c",@"e",@"b",@"c",@"e",@"d",@"g"],
                     @[@"g",@"b",@"c",@"e",@"b",@"c",@"e",@"d",@"a",@"c"],
                     @[@"g",@"b",@"c",@"e",@"b",@"c",@"e",@"d",@"a",@"c",@"e"],
                     @[@"g",@"b",@"c",@"e",@"b",@"c",@"e",@"d",@"a",@"c",@"a",@"b"],
                     @[@"g",@"b",@"c",@"e",@"b",@"c",@"e",@"d",@"a",@"c",@"a",@"b",@"a"],
                     @[@"g",@"b",@"c",@"e",@"b",@"c",@"e",@"d",@"a",@"c",@"a",@"b",@"a",@"14"],
                     @[@"g",@"b",@"c",@"e",@"b",@"c",@"e",@"d",@"a",@"c",@"a",@"b",@"a",@"14",@"15"],
                     @[@"g",@"b",@"c",@"e",@"b",@"c",@"e",@"d",@"a",@"c",@"a",@"b",@"a",@"14",@"15",@"16"],
                     @[@"g",@"b",@"c",@"e",@"b",@"c",@"e",@"d",@"a",@"c",@"a",@"b",@"a",@"14",@"15",@"16",@"17"],
                     @[@"g",@"b",@"c",@"e",@"b",@"c",@"e",@"d",@"a",@"c",@"a",@"b",@"a",@"14",@"15",@"16",@"17",@"18"],
                     @[@"g",@"b",@"c",@"e",@"b",@"c",@"e",@"d",@"a",@"c",@"a",@"b",@"a",@"14",@"15",@"16",@"17",@"18",@"19"],
                     @[@"g",@"b",@"c",@"e",@"b",@"c",@"e",@"d",@"a",@"c",@"a",@"b",@"a",@"14",@"15",@"16",@"17",@"18",@"19",@"20"],
                     @[@"g",@"b",@"c",@"e",@"b",@"c",@"e",@"d",@"a",@"c",@"a",@"b",@"a",@"14",@"15",@"16",@"17",@"18",@"19",@"20",@"21"],
                     @[@"g",@"b",@"c",@"e",@"b",@"c",@"e",@"d",@"a",@"c",@"a",@"b",@"a",@"14",@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22"],
                     @[@"g",@"b",@"c",@"e",@"b",@"c",@"e",@"d",@"a",@"c",@"a",@"b",@"a",@"14",@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22",@"23"],
                     @[@"g",@"b",@"c",@"e",@"b",@"c",@"e",@"d",@"a",@"c",@"a",@"b",@"a",@"14",@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22",@"23",@"24"]
                     
                     ];
        
    }
    return _dataArr;
}

-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[testTableViewCell class] forCellReuseIdentifier:@"cell"];
    }
    return _tableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    testTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.lable.text = [NSString stringWithFormat:@"%ld",indexPath.row];
    
    cell.deleget = self;
    cell.indexPath = indexPath;
    cell.dataArr = self.dataArr[indexPath.row];
    return cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.dicH[indexPath]) {
        NSNumber *num = self.dicH[indexPath];
        return [num floatValue];
    }else{
        return 80;
    }
}

-(void)uodataTableViewCellHight:(testTableViewCell *)cell andHight:(CGFloat)hight andIndexPath:(NSIndexPath *)indexPath{
    
    if (![self.dicH[indexPath] isEqualToNumber: @(hight)]) {
        self.dicH[indexPath] = @(hight);
        NSLog(@"indexPath.row = %ld",indexPath.row);
        NSLog(@"高度 = %lf",[@(hight) floatValue]);
        [self.tableView reloadData];
    }
}

#pragma mark 下面是檢測流暢度
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
    [self setupUI];
    _displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(displayAction:)];
    [_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
    
}

-(void)dealloc
{
    [_displayLink removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
    _displayLink = nil;
}

-(void)setupUI
{
    _numLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 20)];
    _numLabel.text = @"60";
    _numLabel.textColor = [UIColor greenColor];
    _numLabel.backgroundColor = [UIColor blackColor];
    [self.view addSubview:_numLabel];
}

-(void)displayAction:(id)sender
{
    if (_lastTime == 0) {
        _lastTime = _displayLink.timestamp;
        return;
    }
    
    _count++;
    NSTimeInterval delta = _displayLink.timestamp - _lastTime;
    if (delta < 1) return;
    _lastTime = _displayLink.timestamp;
    float fps = _count / delta;
    _count = 0;
    
    NSString *text = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%d FPS", (int)round(fps)]];
    _numLabel.text = text;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark 保存cell高度
- (NSMutableDictionary *)dicH {
    if(_dicH == nil) {
        _dicH = [[NSMutableDictionary alloc] init];
    }
    return _dicH;
}
@end

testTableViewCell.h

//
//  testTableViewCell.h
//  tableViewCell中加載collectionView
//
//  Created by 彭成 on 2018/12/27.
//  Copyright ? 2018年 SJJ. All rights reserved.
//

#import <UIKit/UIKit.h>

@class testTableViewCell;
@protocol testTableViewCellDelegate <NSObject>

-(void)uodataTableViewCellHight:(testTableViewCell*)cell andHight:(CGFloat)hight andIndexPath:(NSIndexPath *)indexPath;
@end

@interface testTableViewCell : UITableViewCell
@property(nonatomic,strong)NSIndexPath *indexPath;
@property(nonatomic,strong)NSArray *dataArr;
@property(nonatomic,weak) id<testTableViewCellDelegate>deleget;
@property(nonatomic,strong)UILabel *lable;
@end

testTableViewCell.m

//
//  testTableViewCell.m
//  tableViewCell中加載collectionView
//
//  Created by 彭成 on 2018/12/27.
//  Copyright ? 2018年 SJJ. All rights reserved.
//

#import "testTableViewCell.h"
#import "textCollectionViewCell.h"
#import <Masonry.h>

@interface testTableViewCell ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,assign)CGFloat hightED;
@end

@implementation testTableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self.hightED = 0.0;
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
        [self.contentView addSubview:self.lable];
        
        [self.contentView addSubview:self.collectionView];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0);
            make.left.mas_equalTo(50);
            make.right.mas_equalTo(-50);
            make.height.mas_equalTo(50);//先隨定一個
        }];
    }
    return self;
}

-(UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.scrollDirection =     UICollectionViewScrollDirectionVertical;
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        
        [_collectionView registerClass:[textCollectionViewCell class] forCellWithReuseIdentifier:@"123"];
        _collectionView.backgroundColor = [UIColor yellowColor];
        
    }
    return _collectionView;
}

-(void)setDataArr:(NSArray *)dataArr{
    [self.collectionView reloadData]; //重新?lián)Q數(shù)據(jù)源的時候 記得重回用的cell上的colletionView重新加載數(shù)據(jù)
    self.hightED = 0; //當(dāng)重新?lián)Q數(shù)據(jù)源的時候 初始化自己的高度. (如果不寫 就有一種意外比如 比如一個cell被重用,開始這個cell的collectionView的cell 和重用之后是一樣的  self.hightED != hight  重用之前 和重用之后的內(nèi)容高度 很定是一樣的啊 那么他的高度是不用跟新 但是更新tableViewCell的高度的 代理方法還是 要走吧)
    _dataArr = dataArr;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (self.dataArr.count == 0) {
        return 1;
    }else{
        return self.dataArr.count;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    textCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"123" forIndexPath: indexPath];
    cell.backgroundColor = [UIColor blackColor];
    
    [self updateCollectionViewHight:self.collectionView.collectionViewLayout.collectionViewContentSize.height];
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake(40, 60);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(10, 10, 10, 10);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    
    return 10;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    
    return 10;
}


-(void)updateCollectionViewHight:(CGFloat)hight{
    
    NSLog(@"+%@",self);
    NSLog(@"++ %f",self.hightED);
    NSLog(@"+++ %f",hight);
    
    if (self.hightED != hight) { //這個判斷起到兩個作用 第一 以為這個方法被調(diào)用多次這樣寫 保證 每個cell里面調(diào)用一次,切只調(diào)用一次  第二是當(dāng)cell被重用從用的cell上的collectionView內(nèi)容高度不一樣的時候重新 更新跟新高度
        self.hightED = hight;
        
        NSLog(@"+++++%ld",self.indexPath.row);
        [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(hight);
        }];
        
        if (_deleget && [_deleget respondsToSelector:@selector(uodataTableViewCellHight:andHight:andIndexPath:)]) {
            [self.deleget uodataTableViewCellHight:self andHight:hight andIndexPath:self.indexPath];
        }
    }
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}
@end

textCollectionViewCell.h

//
//  textCollectionViewCell.h
//  tableViewCell中加載collectionView
//
//  Created by 彭成 on 2018/12/27.
//  Copyright ? 2018年 SJJ. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface textCollectionViewCell : UICollectionViewCell
@property(nonatomic,strong)NSString *imageName;
@end

textCollectionViewCell.m

//
//  textCollectionViewCell.h
//  tableViewCell中加載collectionView
//
//  Created by 彭成 on 2018/12/27.
//  Copyright ? 2018年 SJJ. All rights reserved.
//

#import "textCollectionViewCell.h"

@interface textCollectionViewCell ()
@property(nonatomic,strong)UIImageView *imageView;

@end

@implementation textCollectionViewCell
-(instancetype)initWithFrame:(CGRect)fram{
    if (self = [super initWithFrame:fram]) {
        self.imageView  = [[UIImageView alloc]init];
        self.backgroundView = self.imageView;
        self.imageView.image = [UIImage imageNamed:@"a"];
    }
    return self;
}

-(void)setImageName:(NSString *)imageName{
    _imageName  = imageName;
    self.imageView.image = [UIImage imageNamed:_imageName];
}
@end

以上均為學(xué)習(xí)筆記 摘抄至 cell嵌套collectionView

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末儒恋,一起剝皮案震驚了整個濱河市睁冬,隨后出現(xiàn)的幾起案子德崭,更是在濱河造成了極大的恐慌,老刑警劉巖锭沟,帶你破解...
    沈念sama閱讀 221,695評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異陪捷,居然都是意外死亡踩窖,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評論 3 399
  • 文/潘曉璐 我一進(jìn)店門帜羊,熙熙樓的掌柜王于貴愁眉苦臉地迎上來咒程,“玉大人,你說我怎么就攤上這事讼育≌室觯” “怎么了?”我有些...
    開封第一講書人閱讀 168,130評論 0 360
  • 文/不壞的土叔 我叫張陵奶段,是天一觀的道長饥瓷。 經(jīng)常有香客問我,道長忧饭,這世上最難降的妖魔是什么扛伍? 我笑而不...
    開封第一講書人閱讀 59,648評論 1 297
  • 正文 為了忘掉前任,我火速辦了婚禮词裤,結(jié)果婚禮上刺洒,老公的妹妹穿的比我還像新娘鳖宾。我一直安慰自己,他們只是感情好逆航,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,655評論 6 397
  • 文/花漫 我一把揭開白布鼎文。 她就那樣靜靜地躺著,像睡著了一般因俐。 火紅的嫁衣襯著肌膚如雪拇惋。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,268評論 1 309
  • 那天抹剩,我揣著相機(jī)與錄音撑帖,去河邊找鬼。 笑死澳眷,一個胖子當(dāng)著我的面吹牛胡嘿,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播钳踊,決...
    沈念sama閱讀 40,835評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼衷敌,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了拓瞪?” 一聲冷哼從身側(cè)響起缴罗,我...
    開封第一講書人閱讀 39,740評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎祭埂,沒想到半個月后面氓,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,286評論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡蛆橡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,375評論 3 340
  • 正文 我和宋清朗相戀三年侧但,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片航罗。...
    茶點(diǎn)故事閱讀 40,505評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖屁药,靈堂內(nèi)的尸體忽然破棺而出粥血,到底是詐尸還是另有隱情,我是刑警寧澤酿箭,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布复亏,位于F島的核電站,受9級特大地震影響缭嫡,放射性物質(zhì)發(fā)生泄漏缔御。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,873評論 3 333
  • 文/蒙蒙 一妇蛀、第九天 我趴在偏房一處隱蔽的房頂上張望耕突。 院中可真熱鬧笤成,春花似錦、人聲如沸眷茁。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,357評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽上祈。三九已至培遵,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間登刺,已是汗流浹背籽腕。 一陣腳步聲響...
    開封第一講書人閱讀 33,466評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留纸俭,地道東北人皇耗。 一個月前我還...
    沈念sama閱讀 48,921評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像掉蔬,于是被迫代替她去往敵國和親廊宪。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,515評論 2 359

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

  • Swift1> Swift和OC的區(qū)別1.1> Swift沒有地址/指針的概念1.2> 泛型1.3> 類型嚴(yán)謹(jǐn) 對...
    cosWriter閱讀 11,111評論 1 32
  • 買來葉孤城的電子書看到書中有這么一個場景,每個cell上有不定個按鈕或者View.怎嘛寫好呢?for循環(huán)布局?一個...
    大墻66370閱讀 9,750評論 37 54
  • 2017.02.22 可以練習(xí)女轿,每當(dāng)這個時候箭启,腦袋就犯困,我這腦袋真是神奇呀蛉迹,一說讓你做事情傅寡,你就犯困,你可不要太...
    Carden閱讀 1,348評論 0 1
  • 1.安裝web3j命令行工具 brew tap web3j/web3j brew install web3j 2....
    zbcao閱讀 1,155評論 0 0
  • 文 / 天籟 我在兩年以前就渴望著自己能學(xué)習(xí)插畫北救,在一直以來的概念中荐操,插畫很難學(xué)。 每當(dāng)我看見有人能用少許的筆墨就...
    漫步光之河閱讀 271評論 2 0