在做tableView的时候,我们有时候需要根据model里面的某个成员变量是否存在来动态调整cell的高度,最近在网上看到一段代码不错,跟大家Share一下。
在
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
类中获取cell的高度:
[objc]
view plaincopyCGSize boundSize = CGSizeMake(216, CGFLOAT_MAX);
cell.textLabel.text = @"12345678900123456789";
cell.userInteractionEnabled = NO;
cell.textLabel.numberOfLines = 0;
CGSize requiredSize = [cell.textLabel.text sizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:boundSize lineBreakMode:UILineBreakModeWordWrap];
CGRect rect = cell.frame;
rect.size.height = requiredSize.height+5;
cell.frame = rect;
这时候获取到了cell的高度,然后在- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
类中改变cell的高度:
[objc]
view plaincopyUITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
NSLog(@"cell height %f",cell.frame.size.height);
return cell.frame.size.height;
这样以来cell的高度自动改变啦。
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。