1

我正在使用这种流行的方法来自动调整 UITableViewCell 中的内容大小。它不工作。我得到的是:

在此处输入图像描述

我的应用程序是一个通用应用程序,我正在使用带有情节提要的 iOS 5。这是我的 .h 文件代码:

@interface SecondMasterViewController : UITableViewController

对于这两种方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
        cell.textLabel.numberOfLines = 0;
        cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0];
    }
    cell.textLabel.text = [self.mainCategories objectAtIndex:indexPath.row];
    UIImage *myAwesomeCellImage;
    myAwesomeCellImage = [UIImage imageNamed:
                          [NSString stringWithFormat:@"%@%@",
                           cell.textLabel.text,@".png"]];
    cell.imageView.image = myAwesomeCellImage;

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellText = @"Go get some text for your cell.";
    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:17.0];
    CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
    CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

    return labelSize.height + 20;
}

我做错了什么?我尝试了不同的方法,但都不起作用。

提前致谢!

4

1 回答 1

2

@"Go get some text for your cell."在你的? 中使用文字字符串-tableView:heightForRowAtIndexPath:?我认为你应该用单元格的实际文本替换它,这样你就可以调整它的大小,看看单元格需要多高。[self.mainCategories objectAtIndex:indexPath.row]尝试用(您为单元格文本设置的内容)替换该字符串,看看它是否有帮助。

于 2012-09-10T22:03:04.570 回答