今天在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