自动布局(FDTemplateLayoutCell的使用)

今天看到了@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];
            }];
        
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值