0

我正在做这个项目。目前我面临一个问题,我在一个 ViewController 中有 3 个 UITableView。它看起来像这样:

在此处输入图像描述

现在,第一个 TableView(顶部)只有一行,大小为 55 像素,不必调整大小。接下来,第二个是 TableView,同样有一行,但是这一行必须根据它的内容来调整大小。现在这个 TableView 显示这一行:

在此处输入图像描述

此行必须调整大小取决于其中文本的长度。我计算大小的方法是:

if(tableView == self.usersPostOnTheWallTableView) {
    NSDictionary *propertyItems = [self.repliesItems objectAtIndex:indexPath.row];

    NSString *text = [self.postItems objectForKey:@"text"];

    NSLog(@"%@", text);

    CGSize constraintSize = CGSizeMake(280, MAXFLOAT);

    CGSize sizeText = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

    self.height = MAX(sizeText.height, 44.0f);

    self.photosArray = [propertyItems objectForKey:@"attach_photos"];
    self.videosArray = [propertyItems objectForKey:@"attach_videos"];

    if((self.photosArray.count == 0) && (self.videosArray.count == 0)) {

        height += (CELL_CONTENT_MARGIN * 2);

    } else if ((self.videosArray.count > 0) && (self.photosArray.count == 0)) {

        height += 90 + (CELL_CONTENT_MARGIN * 2);

    } else if((self.photosArray.count > 0) && (self.videosArray.count == 0)) {

        height += 85 + (CELL_CONTENT_MARGIN * 2);
    }

}

现在,如果文本非常小,它会很好地适合行,并且行会调整大小。比如这样:

在此处输入图像描述

但是,在文本变大之后,会发生以下情况:

在此处输入图像描述

在此处输入图像描述

显然我不需要在那里滚动,我会禁用它。但这里的问题是它需要文本的大小,并调整标签的大小,你可以看到它从标签的顶部和底部产生了多少空间。但它并没有把实际的文字放在那里。另一件事是它没有自行调整 TableView 的大小,当我尝试调整它的大小时,我得到了中间表与底部表的重叠。

任何人都可以推荐任何解决方案,或者可能是另一种实现方式吗?

提前致谢!

4

1 回答 1

1

你可以这样做是使用 tableviewDelegate 方法根据您的文本设置单元格高度,例如:

将为每个单元调用此方法以决定单元应具有多少高度

  - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
 {
    NSString *strDetails = [yourTextArray objectAtIndex:indexPath.row];
   //if(indexPath.row == 1)
   //{
     CGSize stringSize = [strDetails sizeWithFont:[UIFont fontWithName:@"Marker Felt" size:13] constrainedToSize:CGSizeMake(320, 9999) lineBreakMode:UILineBreakModeWordWrap]; // Provide text as well as font name in which your text will be displayed and constrainedToSize which defines width and height it should be maximum
     return stringSize.height+65; //Here 65 can be any value depending on Calculation  ImageView height + uppermost space + bottom space added
   //}
   //else 
   //{
   //  return 80 // anything u wishes
   //}
 }
于 2012-07-26T09:13:56.230 回答