ios - 无法使 UITableView 单元格高度与文本量成比例

标签 ios ios5 uitableview

我正在尝试让 UITableView 单元格适合文本。到目前为止,我有这样的事情:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UILabel *label = nil;

    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault)
            reuseIdentifier:@"business"];

    NSString *comment = [[items_array objectAtIndex:[indexPath row]] objectForKey:(@"comment")];

cell.textLabel.numberOfLines = 0;
        cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;

        CGSize constraint = CGSizeMake(320 - (10 * 2), 20000.0f);

        CGSize size = [comment sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];


        label = [[UILabel alloc] initWithFrame:CGRectZero];

        [label setText:comment];
        [label setFrame:CGRectMake(10, 10, 320 - (10 * 2), MAX(size.height, 44.0f))];

        cell.textLabel.text = comment;  
    }

我也有这个功能

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{    
    NSString *comment = [[items_array objectAtIndex:[indexPath row]] objectForKey:(@"comment")];

    CGSize constraint = CGSizeMake(320 - (10 * 2), 20000.0f);

    CGSize size = [comment sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

    CGFloat height = MAX(size.height, 44.0f);

    return height + (10 * 2);
}

但它没有为 UITableView 的单元格分配正确的高度。我认为我在分配标签和单元格文本的方式/位置上犯了一些错误。

请帮助我理解它应该如何。

谢谢!

最佳答案

这会让你们更亲近吗?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"business"]; 

 if (cell == nil)
 {  
  cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault)reuseIdentifier:@"business"]; 
 }

 NSString *comment = [[items_array objectAtIndex:[indexPath row]] objectForKey:(@"comment")]; 

 CGSize constraint = CGSizeMake(320 - (10 * 2), 20000.0f); 

 CGSize size = [comment sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; 

 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, MAX(size.height, 44.0f) + 20.0f)]; 
 label.numberOfLines = 0; 
 label.lineBreakMode = UILineBreakModeWordWrap; 
 label.text = comment;  

 [cell.contentView addSubview:label]; 

} 

关于ios - 无法使 UITableView 单元格高度与文本量成比例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11851789/

相关文章:

android - 如何在我的 Flutter 应用程序中从右侧导航添加幻灯片?

objective-c - Storyboard - 具有相同自定义单元格的多个 UITableView Controller

ios - 如何在不复制图像的情况下将 exif 元数据注入(inject)图像?

ios - 键盘隐藏时 UITableView 略微上升

ios - WKWebView 获取cookie

objective-c - MPMoviePlayerController 不适用于 RemoteControlReceivedWithEvent

ios - 数据未在详细 View 中加载

ios - NSMutableArray 到 UITableView

ios - 当tableView高度为真实高度时

objective-c - 检测到 "multitasking bar"被触发