使用第三方UITableView+FDTemplateLayoutCell计算cell行高注意点

现在很方便的计算单元格的行高大部分都是使用的第三方框架UITableView+FDTemplateLayoutCell,不知道你在使用这个框架的时候有没有遇到和我一样的问题,比如:

在这样计算cell的高度的时候崩溃了.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return [tableView fd_heightForCellWithIdentifier:PersonDataCellIdentify configuration:^(id cell) {
        [self configurePersonDataTableViewCell:cell atIndexPath:indexPath];
    }];
}

 

崩溃原因是必须注册cell......然后反复检查代码.

1.自己明明都有写注册cell的代码,,identify也木有错误,就是不知道为什么崩溃;

2.以为是cell的布局有问题,导致不能自己计算单元格的行高;

其实原因很简单,不知道你得问题是啥,总之我的问题就是一行代码的位置问题.

原因就是:

    self.tableView.tableFooterView = [[UIView alloc]init];这句代码在注册cell之前,导致的崩溃.
因为你在设置表视图的tableFooterView的时候,他就会走tableView的数据源和代理方法,而这个时候你还没有注册cell,导致他认为你还没有注册cell,导致崩溃.
#pragma mark - 设置界面

- (void)setUI {
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, K_SCREEN_WIDTH, K_SCREEN_HEIGHT) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.tableView.tableFooterView = [[UIView alloc]init];

    // 注册cell
//    [_tableView registerNib:[UINib nibWithNibName:@"AddTableViewCellWithLabel" bundle:nil] forCellReuseIdentifier:ADD_LABEL_CELL_ID];
    [_tableView registerClass:[PersonDataCell class] forCellReuseIdentifier:PersonDataCellIdentify];
    [self.view addSubview:self.tableView];
}

解决办法就是: 先注册cell,再设置tableFooterView就好了....

#pragma mark - 设置界面

- (void)setUI {
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, K_SCREEN_WIDTH, K_SCREEN_HEIGHT) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);

    // 注册cell
//    [_tableView registerNib:[UINib nibWithNibName:@"AddTableViewCellWithLabel" bundle:nil] forCellReuseIdentifier:ADD_LABEL_CELL_ID];
    [_tableView registerClass:[PersonDataCell class] forCellReuseIdentifier:PersonDataCellIdentify];
    self.tableView.tableFooterView = [[UIView alloc]init];
    [self.view addSubview:self.tableView];
}

持续记录自己在工作中遇到的问题和解决方法,希望学到更多的知识.

posted on 2017-03-18 09:20  玉思盈蝶  阅读(348)  评论(0编辑  收藏  举报

导航