0

我试图让 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);
}

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

请帮助我理解它应该是怎样的。

谢谢!

4

2 回答 2

1

You need to set height of a cell using -tableView:heightForRowAtIndexPath.


With the new code posted

Here is how I did it (I used a prototype cell so things may be different for you).

I set the the autosizing of the cell to auto size the height of the label (this may already be set for the textLabel).

Then in -tableView:heightForRowAtIndexPath I did the following:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{    
    CGSize size = CGSizeMake(WIDTH_OF_PROTOTYPE_LABEL, CGFLOAT_MAX);
    size = [TEXT_AT_INDEX_PATH sizeWithFont:FONT_OF_PROTOTYPE_LABEL constrainedToSize:size lineBreakMode:UILineBreakModeWordWrap];

    CGFloat height = size.height + (HEIGHT_OF_PROTOTYPE_CELL - HEIGHT_OF_PROTOTYPE_LABEL);
    return MAX(height, HEIGHT_OF_PROTOTYPE_CELL);
}

Update 2

In looking at your code again, I see two possible problems. 1) You have the width of the textLabel wrong. 2) You should compute the do MAX as the last thing after you compute the final new height.

于 2012-08-07T18:24:06.673 回答
1

这会让你更亲近吗?

- (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]; 

} 
于 2012-08-07T18:46:15.713 回答