UITableView里面嵌套collectionView(摘抄笔记)

在开发中总有这么一些场景,每个cell上有不定个按钮或者View.怎嘛写好呢?for循环布局?一个cell上如果很多view呢?......
把collectionView放在cell里其实挺好的,还有每个每个cell的点击事件直接可以写在 代理方法中.

效果图

先简要说明一下思路
1创建一个tableView加到控制器上 遵守协议实现代理方法...不拉不拉那一堆事...... 返回一个 固定高度(因为这个时候不知道那个每个cell的高度是多少合适,先固定 以后变)
2重写tableViewCell 在cell中创建 collectionView并且加到cell中 注意 因为collectionView的高度是根据cell来说的,这个时候还不知道cell有多少先随便给一个定值 (后期再改)collectionView用masonry写约束 是 top left right height
问题
怎么得到collectionView的内容视图的的高度呢? 知道collectionView的内容视图的的高度之后改了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]; //重新换数据源的时候 记得重回用的cell上的colletionView重新加载数据
    self.hightED = 0; //当重新换数据源的时候 初始化自己的高度. (如果不写 就有一种意外比如 比如一个cell被重用,开始这个cell的collectionView的cell 和重用之后是一样的  self.hightED != hight  重用之前 和重用之后的内容高度 很定是一样的啊 那么他的高度是不用跟新 但是更新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) { //这个判断起到两个作用 第一 以为这个方法被调用多次这样写 保证 每个cell里面调用一次,切只调用一次  第二是当cell被重用从用的cell上的collectionView内容高度不一样的时候重新 更新跟新高度
        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

以上均为学习笔记 摘抄至 cell嵌套collectionView

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,457评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,837评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,696评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,183评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,057评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,105评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,520评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,211评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,482评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,574评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,353评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,897评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,489评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,683评论 2 335

推荐阅读更多精彩内容

  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,067评论 1 32
  • 买来叶孤城的电子书看到书中有这么一个场景,每个cell上有不定个按钮或者View.怎嘛写好呢?for循环布局?一个...
    大墙66370阅读 9,739评论 37 54
  • 2017.02.22 可以练习,每当这个时候,脑袋就犯困,我这脑袋真是神奇呀,一说让你做事情,你就犯困,你可不要太...
    Carden阅读 1,306评论 0 1
  • 1.安装web3j命令行工具 brew tap web3j/web3j brew install web3j 2....
    zbcao阅读 1,122评论 0 0
  • 文 / 天籁 我在两年以前就渴望着自己能学习插画,在一直以来的概念中,插画很难学。 每当我看见有人能用少许的笔墨就...
    漫步光之河阅读 266评论 2 0