1

UITableViewCellのコンテンツの自動サイズ変更にこの一般的なアプローチを使用しています。そして、それは機能していません。私が得るのはこれだけです:

ここに画像の説明を入力してください

私のアプリはユニバーサルアプリで、ストーリーボード付きのiOS5を使用しています。.hファイルのコードは次のとおりです。

@interface SecondMasterViewController : UITableViewController

そしてこれらの2つの方法のために:

- (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 に答える