cell嵌套collectionView

買(mǎi)來(lái)葉孤城的電子書(shū)看到書(shū)中有這么一個(gè)場(chǎng)景,每個(gè)cell上有不定個(gè)按鈕或者View.怎嘛寫(xiě)好呢?for循環(huán)布局?一個(gè)cell上如果很多view呢?......
把collectionView放在cell里其實(shí)挺好的,還有每個(gè)每個(gè)cell的點(diǎn)擊事件直接可以寫(xiě)在 代理方法中..

先簡(jiǎn)要說(shuō)明一下思路
1創(chuàng)建一個(gè)tableView加到控制器上 遵守協(xié)議實(shí)現(xiàn)代理方法...不拉不拉那一堆事...... 返回一個(gè) 固定高度(因?yàn)檫@個(gè)時(shí)候不知道那個(gè)每個(gè)cell的高度是多少合適,先固定 以后變)

2重寫(xiě)tableViewCell 在cell中創(chuàng)建 collectionView并且加到cell中 注意 因?yàn)閏ollectionView的高度是根據(jù)cell來(lái)說(shuō)的,這個(gè)時(shí)候還不知道cell有多少先隨便給一個(gè)定值 (后期再改)collectionView用masonry寫(xiě)約束 是 top left right height

問(wèn)題
怎么得到collectionView的內(nèi)容視圖的的高度呢? 知道collectionView的內(nèi)容視圖的的高度之后改了collectionView的高度,怎嘛改變 tableViewCell的高度呢?

一切盡在代碼中

控制器.h

//
//  ViewController.h
//  tableViewCell中加載collectionView
//
//  Created by 3D on 16/7/20.
//  Copyright ? 2016年 3D. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@end

控制器.m

 //
 //  ViewController.m
 //  tableViewCell中加載collectionView
 //
 //  Created by 3D on 16/7/20.
 //  Copyright ? 2016年 3D. 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 下面是檢測(cè)流暢度
 - (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 3D on 16/7/20.
//  Copyright ? 2016年 3D. 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 3D on 16/7/20.
//  Copyright ? 2016年 3D. 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);//先隨定一個(gè)
    }];
}
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ù)源的時(shí)候 記得重回用的cell上的colletionView重新加載數(shù)據(jù)
self.hightED = 0; //當(dāng)重新?lián)Q數(shù)據(jù)源的時(shí)候 初始化自己的高度. (如果不寫(xiě) 就有一種意外比如 比如一個(gè)cell被重用,開(kāi)始這個(gè)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) { //這個(gè)判斷起到兩個(gè)作用 第一 以為這個(gè)方法被調(diào)用多次這樣寫(xiě) 保證 每個(gè)cell里面調(diào)用一次,切只調(diào)用一次  第二是當(dāng)cell被重用從用的cell上的collectionView內(nèi)容高度不一樣的時(shí)候重新 更新跟新高度
    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 3D on 16/7/20.
 //  Copyright ? 2016年 3D. All rights reserved.
//

#import <UIKit/UIKit.h>

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

textCollectionViewCell.m

//
//  textCollectionViewCell.m
//  tableViewCell中加載collectionView
//
//  Created by 3D on 16/7/20.
//  Copyright ? 2016年 3D. 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

在最后我有一個(gè)疑問(wèn),為啥模擬器卡的一比,而真機(jī)比較流暢.
哪里寫(xiě)的不好請(qǐng)指出??互相學(xué)習(xí)

6808D59A-6AB8-4190-910F-1D878DE26710.png

運(yùn)行效果圖

2016-07-21 18_15_47.gif
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容