IOS XIB Cell自适应高度实现

1.代码实现Cell高度自适应的方法git

   经过代码来实现,须要计算每一个控件的高度,以后获取一个cell的github

总高度,比较常见的是经过lable的文本计算须要的高度。spa

CGSize labelsize = [@"asdassdas" sizeWithFont:font constrainedToSize:CGSizeMake(320,2000) lineBreakMode:NSLineBreakModeWordWrap];

 这样就能够计算展现须要的高度,cell里面展现的时候能够在代理的方法内放回高度就好了。今天要实现的代理

是经过auto layout实现获取展现须要的高度,实现的具体是使用了一个第三方库(https://github.com/forkingdog/UITableView-FDTemplateLayoutCell),简化了实现,使用这个code

库能够让你专一的设置约束,下载后把UITableView+FDTemplateLayoutCell.h,UITableView+FDTemplateLayoutCell.m拖入项目就好,也能够经过pod 安装。blog

 

2.实现效果it

         

 

 

3.实现代码io

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [arrData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyTableViewCell *cell = [self.tabview dequeueReusableCellWithIdentifier:@"CTF"];
    cell.txt.text = [arrData objectAtIndex:indexPath.row];
    cell.img.image=[UIImage imageNamed:@"a.jpg"];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [tableView fd_heightForCellWithIdentifier:@"CTF" configuration:^(MyTableViewCell *cell) {
        cell.txt.text = [arrData objectAtIndex:indexPath.row];
        cell.img.image=[UIImage imageNamed:@"a.jpg"];
    }];
}

   上传比较麻烦, 须要Demo的能够留邮箱,我会及时发。table

相关文章
相关标签/搜索