【问题标题】:Change dynamic UITextView height inside UITableViewCell更改 UITableViewCell 内的动态 UITextView 高度
【发布时间】:2014-05-19 19:46:52
【问题描述】:

我正在尝试更改 uitableviewcell 内的 UITextView 高度,我在 SO 上进行了很多搜索,并且有很多答案,其中许多非常相似。我认为我找到的最佳答案是:

UITableViewCell with UITextView height in iOS 7?

我试过了,但效果不是很好,这是我的代码:

- (CGFloat)textViewHeightForRowAtIndexPath: (NSIndexPath*)indexPath {
    UITextView *calculationView = [self.textViews objectForKey: indexPath];
    CGFloat textViewWidth;
    if (!calculationView.attributedText) {
        // This will be needed on load, when the text view is not inited yet

        calculationView = [[UITextView alloc] init];
        calculationView.attributedText = [[NSAttributedString alloc] initWithString:[[self.comments objectAtIndex:indexPath.row] valueForKey:@"comment"] attributes:self.regularDict];// get the text from your datasource add attributes and insert here
        textViewWidth = 250; // Insert the width of your UITextViews or include calculations to set it accordingly
    } else {
        textViewWidth = calculationView.frame.size.width;
    }
    CGSize size = [calculationView sizeThatFits:CGSizeMake(textViewWidth, FLT_MAX)];
    return size.height;
}

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

return [self textViewHeightForRowAtIndexPath:indexPath];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CommentCell *cell = (CommentCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    NSAttributedString *text_user = [[NSAttributedString alloc] initWithString:[[self.comments objectAtIndex:indexPath.row] valueForKey:@"comment"] attributes:self.regularDict];

    [cell.my_text setAttributedText:text_user];
    [cell.my_text setTextAlignment:NSTextAlignmentRight];

    CGRect frame = cell.my_text.frame;

        CGSize size = [cell.my_text sizeThatFits:CGSizeMake(250, FLT_MAX)];

frame.size.height = size.height;

    [cell.my_text setFrame:frame];
    [self.textViews setObject:cell.my_text forKey:indexPath];

    return cell;
}

但这是结果:

行中有很多空白,我不明白为什么...

谁能帮帮我?

【问题讨论】:

    标签: ios objective-c uitableview uitextview


    【解决方案1】:

    更多关于你想要达到的目标的信息会有所帮助,但是......

    我想你可能会发现tableView:heightForRowAtIndexPath:tableView:cellForRowAtIndexPath: 之前运行到完成,因此任何更改tableView:(UITableView *)tableView cellForRowAtIndexPath:UITextView 高度的代码都将无效。

    需要明确的是,所有行高都是在UITableView 填充该行内单元格的内容之前设置的。

    所以在使用tableView:heightForRowAtIndexPath: 时,在此方法中确定indexPath 处的每个单元格的高度很重要。

    【讨论】:

      【解决方案2】:

      我曾经遇到过这个问题,最终是因为我使用的字体不需要像“sizeThatFits”返回的那样高的高度。从那时起,每当我需要时,我都会随身携带以下帮助方法:

      - (CGFloat)heightOfTextViewWithString:(NSString *)string 
                                   withFont:(UIFont *)font 
                              andFixedWidth:(CGFloat)fixedWidth
      {
          UITextView *tempTV = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, fixedWidth, 1)];
          tempTV.text = [string uppercaseString];
          tempTV.font = font;
      
          CGSize newSize = [tempTV sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
          CGRect newFrame = tempTV.frame;
          newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
          tempTV.frame = newFrame;
      
          return tempTV.frame.size.height;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-06-20
        • 1970-01-01
        • 2019-10-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多