我正在使用this一种流行的方法来自动调整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;
}

我做错了什么?我尝试了不同的方法,但所有方法均无效。

提前致谢!

最佳答案

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

关于iphone - UITableViewCell上的自动调整大小不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12359906/

10-12 00:33
查看更多