如何调整UITableViewCell的大小以适应其内容?

Dav*_*ser 20 objective-c uitableview ios

我有一个UITableview具有多个可重用的TableViewCells.在一个单元格中,我有一个UITextView,它自己调整大小以适应其内容.现在我"只是"必须调整contentViewTableViewCell的大小,所以我可以阅读while文本.我已经尝试过:

cell2.contentView.bounds.size.height = cell2.discriptionTextView.bounds.size.height; 
Run Code Online (Sandbox Code Playgroud)

要么:

cell2.contentView.frame = CGRectMake(0, cell2.discriptionTextView.bounds.origin.y,     
cell2.discriptionTextView.bounds.size.width,     
cell2.discriptionTextView.bounds.size.height); 
Run Code Online (Sandbox Code Playgroud)

在方法中:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath 
*)indexPath {}  
Run Code Online (Sandbox Code Playgroud)

但它不会起作用.

有谁知道如何做到这一点?

新代码:

    @implementation AppDetail

    CGFloat height;
    …

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {…

    cell2.TextView.text = self.text;
            [cell2.TextView sizeToFit];
            height = CGRectGetHeight(cell2.TextView.bounds);
     …
    }

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

        if (indexPath.row == 0) {
            return 143;
        }
        if (indexPath.row == 1) {
            return height;
        }

        return 0;
    }
Run Code Online (Sandbox Code Playgroud)

Ada*_*amG 18

您只能在tableView:heightForRowAtIndexPath:委托方法中调整UITableViewCell的大小.

您必须估计在加载tableView时为每行调用该方法时文本的大小.

这就是我为解决问题所做的.

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
 {
     NSString * yourText = self.myArrayWithTextInIt[indexPath.row]; // or however you are getting the text
     return additionalSpaceNeeded + [self heightForText:yourText];
 }

 -(CGFloat)heightForText:(NSString *)text
 {
   NSInteger MAX_HEIGHT = 2000;
   UITextView * textView = [[UITextView alloc] initWithFrame: CGRectMake(0, 0, WIDTH_OF_TEXTVIEW, MAX_HEIGHT)];
   textView.text = text;
   textView.font = // your font
   [textView sizeToFit];
   return textView.frame.size.height;
  }
Run Code Online (Sandbox Code Playgroud)

编辑

虽然我使用了这个解决方案一段时间,但我找到了一个更优化的解决方案,我建议使用它,因为它不需要分配整个textView才能工作,并且可以处理大于2000的文本.

-(CGFloat)heightForTextViewRectWithWidth:(CGFloat)width andText:(NSString *)text
{
    UIFont * font = [UIFont systemFontOfSize:12.0f];

    // this returns us the size of the text for a rect but assumes 0, 0 origin
    CGSize size = [text sizeWithAttributes:@{NSFontAttributeName: font}];

    // so we calculate the area
    CGFloat area = size.height * size.width;

    CGFloat buffer = whateverExtraBufferYouNeed.0f;

    // and then return the new height which is the area divided by the width
    // Basically area = h * w
    // area / width = h
    // for w we use the width of the actual text view
    return floor(area/width) + buffer;
}
Run Code Online (Sandbox Code Playgroud)


r_1*_*_19 16

正如@Rob Norback所说,有一种叫做UITableViewAutomaticDimension.

对于Swift,动态调整UITableViewCell内容大小的最简单方法就是添加它.

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
     return UITableViewAutomaticDimension
}
Run Code Online (Sandbox Code Playgroud)


enc*_*ife 14

这是iOS 7+的更新版本更干净(没有额外的方法)

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UIFont * font = [UIFont systemFontOfSize:15.0f];
        NSString *text = [getYourTextArray objectAtIndex:indexPath.row];
        CGFloat height = [text boundingRectWithSize:CGSizeMake(self.tableView.frame.size.width, maxHeight) options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:@{NSFontAttributeName: font} context:nil].size.height;

        return height + additionalHeightBuffer;
    }
Run Code Online (Sandbox Code Playgroud)


小智 8

你需要实施heightForRowAtIndexPath.

假设要在textView中显示的数据存储在NSArray中.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
 CGFloat cellheight = 30; //assuming that your TextView's origin.y is 30 and TextView is the last UI element in your cell

 NSString *text = (NSString *)[textArray objectAtIndex:indexpath.row];
 UIFont *font = [UIFont systemFontOfSize:14];// The font should be the same as that of your textView
 CGSize constraintSize = CGSizeMake(maxWidth, CGFLOAT_MAX);// maxWidth = max width for the textView

 CGSize size = [text sizeWithFont:font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

 cellHeight += size.height; //you can also add a cell padding if you want some space below textView

}
Run Code Online (Sandbox Code Playgroud)