今天看到了@SUNNYXX大神的博客,其中有一篇优化UITableViewCell高度计算的那些事讲了FDTemplateLayoutCell,并且与其它两个布局方法做了对比,好处显而易见(最低适配iOS6、运行效率更高)。分别是iOS6的-systemLayoutSizeFittingSize:
的API和iOS8的self-sizing cell
(estimatedRowHeight、UITableViewAutomaticDimension)方法。上面两个方法之前也在博客中写过。
作为一个开发还没到一年的人,@SUNNYXX大神、喵神等都是学习的榜样,各种崇拜。
不同与Xib+FDTemplateLayoutCell的布局方法,我用的是Masonry+FDTemplateLayoutCell,运行时总是有很多约束冲突,对于原因暂时还不知道。
ViewController
#import "ViewController.h"
#import "CustomCell.h"
#import "Masonry.h"
#import "UITableView+FDTemplateLayoutCell.h"
#import "CustomModel.h"
#import "MHPrettyDate.h"
typedef NS_ENUM(NSUInteger, HTSimulatedCacheModel) {
HTSimulatedCacheModelNone=0,
HTSimulatedCacheModelCacheByIndexpath,
HTSimulatedCacheModelCacheByKey
};
#define COMMON_COLOR [UIColor colorWithRed:0/255.0f green:0/255.0f blue:107/255.0f alpha:1.0]
#define REUSEID @"CustomCellId"
@interface ViewController () <UITableViewDataSource,UITableViewDelegate,UIActionSheetDelegate>
//@property(nonatomic,retain) UIRefreshControl *kRefreshControl;
@property(nonatomic,retain) UISegmentedControl *kSegmentedControl;
@property(nonatomic,retain) UITableView *kTabelView;
@property(nonatomic,retain) NSMutableArray *kDataSource;
@end
@implementation ViewController
#pragma mark - Life Cycle
- (void)viewDidLoad {
[super viewDidLoad];
[self uiConfig];
[self buildTestDataThen:^{
[self.kTabelView reloadData];
NSLog(@"count=%ld",self.kDataSource.count);
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [self.kDataSource count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSArray *tempArr=[self.kDataSource objectAtIndex:section];
return [tempArr count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
CustomCell *cell=[tableView dequeueReusableCellWithIdentifier:REUSEID forIndexPath:indexPath];
[self configureCell:cell atIndexpath:indexPath];
return cell;
}
-(void)configureCell:(CustomCell *)cell atIndexpath:(NSIndexPath *)indexPath{
cell.fd_enforceFrameLayout=NO;
if (indexPath.row%2==0) {
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
}else{
cell.accessoryType=UITableViewCellAccessoryCheckmark;
}
NSArray *tempArr=[self.kDataSource objectAtIndex:indexPath.section];
[cell setModel:[tempArr objectAtIndex:indexPath.row]];
}
#pragma mark - UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
HTSimulatedCacheModel model=self.kSegmentedControl.selectedSegmentIndex;
switch (model) {
case HTSimulatedCacheModelNone:
return [tableView fd_heightForCellWithIdentifier:REUSEID configuration:^(id cell) {
[self configureCell:cell atIndexpath:indexPath];
}];
case HTSimulatedCacheModelCacheByIndexpath:
return [tableView fd_heightForCellWithIdentifier:REUSEID cacheByIndexPath:indexPath configuration:^(id cell) {
[self configureCell:cell atIndexpath:indexPath];
}];