Cell中嵌入UICollectionView

今天在CodeReview上看到一篇关于叶孤城关于如何在Cell中有不定数量个带图Button的情况下,保持性能和代码可读性文章,自己也尝试了一下。
link:http://reviewcode.cn/article.html?reviewId=15
代码如下:

#import "ViewController.h"
#import "CustumTableViewCell.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableDictionary *hightDict;
@property(nonatomic,strong)UILabel *numLabel;
@property(nonatomic,strong)CADisplayLink *displayLink;

@property(nonatomic,assign)NSTimeInterval lastTime;
@property(nonatomic,assign)NSInteger count;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [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
{
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:_tableView];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [_tableView registerNib:[UINib nibWithNibName:@"CustumTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
    _numLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 60, 100, 20)];
    _numLabel.text = @"60";
    _numLabel.textColor = [UIColor greenColor];
    [self.view addSubview:_numLabel];
}
                    
#pragma mark ============Action===============

-(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;
}

#pragma mark ============Delegate==============

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 40;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustumTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    self.hightDict[@(indexPath.row)] = @([cell bindHightWithData:indexPath.row]);
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSNumber *hight = self.hightDict[@(indexPath.row)];
    return hight?[hight floatValue]:121;
}

#pragma mark ============GetMethod===============

-(NSMutableDictionary*)hightDict
{
    if (!_hightDict) {
        _hightDict = [NSMutableDictionary dictionary];
    }
    return _hightDict;
}
@end

UITableViewCell中嵌入UICollectionView代码如下:

#import "CustumTableViewCell.h"
#import "CustumCollectionViewCell.h"

@implementation CustumTableViewCell

- (void)awakeFromNib {
    
    _collectView.dataSource = self;
    _collectView.delegate = self;
    _collectView.backgroundColor = [UIColor whiteColor];
    [_collectView registerNib:[UINib nibWithNibName:@"CustumCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
}

-(CGFloat)bindHightWithData:(NSInteger)index
{
    _index = index + 1;
    [_collectView reloadData];
    _collectViewHightCos.constant = _collectView.collectionViewLayout.collectionViewContentSize.height;
    [self setNeedsUpdateConstraints];
    [self updateConstraintsIfNeeded];
    return 21 + _collectViewHightCos.constant;
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _index ;
}

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    return cell;
}
@end

真机测试运行效果如下,留意fps帧数基本稳定在60样子,偶尔也出现了58,基本还算稳定。
demo:https://github.com/jiangtaidi/CollectionViewInCellDemo.git

88.gif

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,259评论 4 61
  • WebSocket-Swift Starscream的使用 WebSocket 是 HTML5 一种新的协议。它实...
    香橙柚子阅读 24,132评论 8 183
  • 安装破解那些事有段时间了,大家有遇到问题的可以给我留言,整体来讲,从Mac过度到win10 还是有些不舒服的,不过...
    Transnet2014阅读 493评论 0 1
  • 薇薇20170901(第1天) 【一个目标】2017-12-31号前收获10万 【今日种下的与此目标相关的种子】 ...
    美宇阅读 248评论 0 0
  • 在iOS开发过程中,不可避免的要和证书打交道,真机调试、App上架、打包给测试去测试等都需要搞证书。在此过程...
    進无尽阅读 2,915评论 2 6