ios - 设置自定义 UITableViewCells 高度

我正在使用一个自定义 UITableViewCell,它有一些要显示的标签、按钮和 ImageView 。单元格中有一个标签,其文本是 NSString 对象,并且字符串的长度可以是可变的。因此,我无法在 UITableViewheightForCellAtIndex 方法中为单元格设置恒定高度。单元格的高度取决于标签的高度,可以使用 NSStringsizeWithFont 方法确定。我尝试使用它,但看起来我在某个地方出错了。怎么解决?

这是用于初始化单元格的代码。

if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier])
{
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    UIImage *image = [UIImage imageNamed:@"dot.png"];
    imageView = [[UIImageView alloc] initWithImage:image];
    imageView.frame = CGRectMake(45.0,10.0,10,10);

    headingTxt = [[UILabel alloc] initWithFrame:   CGRectMake(60.0,0.0,150.0,post_hdg_ht)];
    [headingTxt setContentMode: UIViewContentModeCenter];
    headingTxt.text = postData.user_f_name;
    headingTxt.font = [UIFont boldSystemFontOfSize:13];
    headingTxt.textAlignment = UITextAlignmentLeft;
    headingTxt.textColor = [UIColor blackColor];

    dateTxt = [[UILabel alloc] initWithFrame:CGRectMake(55.0,23.0,150.0,post_date_ht)];
    dateTxt.text = postData.created_dtm;
    dateTxt.font = [UIFont italicSystemFontOfSize:11];
    dateTxt.textAlignment = UITextAlignmentLeft;
    dateTxt.textColor = [UIColor grayColor];

    NSString * text1 = postData.post_body;
    NSLog(@"text length = %d",[text1 length]);
    CGRect bounds = [UIScreen mainScreen].bounds;
    CGFloat tableViewWidth;
    CGFloat width = 0;
    tableViewWidth = bounds.size.width/2;
    width = tableViewWidth - 40; //fudge factor
    //CGSize textSize = {width, 20000.0f}; //width and height of text area
    CGSize textSize = {245.0, 20000.0f}; //width and height of text area
    CGSize size1 = [text1 sizeWithFont:[UIFont systemFontOfSize:11.0f]
                        constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];

    CGFloat ht = MAX(size1.height, 28);
    textView = [[UILabel alloc] initWithFrame:CGRectMake(55.0,42.0,245.0,ht)];
    textView.text = postData.post_body;
    textView.font = [UIFont systemFontOfSize:11];
    textView.textAlignment = UITextAlignmentLeft;
    textView.textColor = [UIColor blackColor];
    textView.lineBreakMode = UILineBreakModeWordWrap;
    textView.numberOfLines = 3;
    textView.autoresizesSubviews = YES;

    [self.contentView addSubview:imageView];
    [self.contentView addSubview:textView];
    [self.contentView addSubview:webView];
    [self.contentView addSubview:dateTxt];
    [self.contentView addSubview:headingTxt];
    [self.contentView sizeToFit];

    [imageView release];
    [textView release];
    [webView release];
    [dateTxt release];
    [headingTxt release];
}

这是高度和宽度出错的标签:

textView = [[UILabel alloc] initWithFrame:CGRectMake(55.0,42.0,245.0,ht)];

最佳答案

你的 UITableViewDelegate 应该实现 tableView:heightForRowAtIndexPath:

Objective-C

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [indexPath row] * 20;
}

swift 5

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return indexPath.row * 20
}

您可能希望使用 NSStringsizeWithFont:constrainedToSize:lineBreakMode: 方法来计算行高,而不仅仅是在 indexPath 上执行一些愚蠢的数学运算 :)

https://stackoverflow.com/questions/494562/

相关文章:

ios - 我可以强制 UITableView 隐藏空单元格之间的分隔符吗?

ios - setNeedsLayout 与 setNeedsUpdateConstraints 和

ios - 使用 NSURLSession 发送 POST 请求

ios - 您可以将 UIGestureRecognizer 附加到多个 View 吗?

objective-c - 有没有办法从 UITableView 中删除分隔线?

ios - 查看保存的 NSUserDefaults 的简单方法?

objective-c - GCD 在主线程中执行任务

objective-c - 在 Storyboard中,如何制作用于多个 Controller 的自

ios - 如何以毫秒为单位准确记录方法的执行时间?

ios - 如果模态 ViewController 演示样式为 UIModalPresentatio