【问题标题】:iOS Objective-C dynamic cell height iOS 7 issueiOS Objective-C 动态单元格高度 iOS 7 问题
【发布时间】:2016-07-30 14:30:23
【问题描述】:

我正在使用 iOS 7 设备创建应用程序。此应用程序包含带有动态单元格高度UITableView

要创建它,我有一个包含方法的自定义单元格:

- (CGFloat)heightWithModel:(Model *)model
{
    CGFloat height = 0.0f;

    height = self.contentLabel.frame.origin.y + [self contentHeightWithModel:model] + CellBottomOffset;

    return height;
}

还有……

- (CGFloat)contentHeightWithModel:(Model *)model
{
    CGFloat height = 0.0;

    NSDictionary *attributes = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14.0f] forKey:NSFontAttributeName];
    NSString *string = model.content;
    NSStringDrawingContext *context = nil;
    NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin;
    CGSize size = CGSizeMake(self.contentLabel.bounds.size.width, CGFLOAT_MAX);

    CGRect frame = [string boundingRectWithSize:size options:options attributes:attributes context:context];

    height = frame.size.height;

    return height;
}

在我的视图控制器中,我实现了UITableViewDelegate协议方法:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height;

    static Cell *cell;

    if (!cell)
    {
        cell = [tableView dequeueReusableCellWithIdentifier:CellReuseIdentifier];
    }

    height = [cell heightWithModel:[self.dataSource.models objectAtIndex:indexPath.row]];

    return height;
}

据我了解,这应该足以创建具有动态单元格高度的表格视图。尽管如此,我有一个像这样的表格视图:

如您所见,部分文本被隐藏,因为标签(红色的)高度太小​​。使用自动布局(距底部 10 像素)动态设置单元格高度。

谁能看出问题出在哪里?

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

    先取一个变量

    CGF浮动高度;

    将此行放入 viewdidload 方法

    在该单元格的估计高度和该标签中,在检查器面板中将行设置为 0,并将其高度 = 更改为 >= 并从所有方面进行约束

    [self.tbl_rating layoutIfNeeded];
    
      [self.tbl_rating setNeedsLayout];
    
    if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1 )
    {
        self.tbl_rating.estimatedRowHeight=153.0f;
        self.tbl_rating.rowHeight=UITableViewAutomaticDimension;
    }
    

    现在把这两种方法用于查找标签高度和动态单元格高度增量

    在此方法中设置您从数组或网络服务中检索的标签文本

     - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 7.1>
    
    if (NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_7_1 ) {
    
    height=[self findHeightForText:[NSString stringWithFormat:@"%@",[[arr_review valueForKey:@"desc"]objectAtIndex:indexPath.row]] havingWidth:self.view.frame.size.width andFont:[UIFont systemFontOfSize:12.0f]].height;
    
         return 153+height;
    
    }   
    return UITableViewAutomaticDimension;
    
    
     }
    
    
    
    
    -(CGSize)findHeightForText:(NSString *)text havingWidth:(CGFloat)widthValue andFont:(UIFont *)font {
    
    
    CGSize size = CGSizeZero;
    if (text) {
    
    
        CGRect frame = [text boundingRectWithSize:CGSizeMake(widthValue, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName:font } context:nil];
    
    
        size = CGSizeMake(frame.size.width, ceil(frame.size.height));
    }
    
    
    return size; 
    
    }
    

    注意:在 tableview 方法中,heightForRowAtIndexPath 我建议您从数组值中传递文本。如果这件事不起作用,请检查您的约束。

    这东西对我有用,希望它会起作用。 谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-13
      • 2015-02-24
      • 2017-07-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-11
      • 1970-01-01
      相关资源
      最近更新 更多