0

各投稿にフルタイトルを複数行で表示し、各セルにテキストタイトルの文字数に応じて独自のカスタム高さを持たせたい. 私はこれをしましたが、うまくいきません。すべてのオブジェクトを個別に呼び出す必要があると思います。どうすればこれを機能させることができますか?ここに私のコードがあります:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    PFObject *object1 = [PFObject objectWithClassName:@"Posts"];
    NSString *string1 = [object1 objectForKey:@"text"];
    NSLog(@"%@",string1);



    CGSize theSize = [string1 sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:CGSizeMake(265.0f, 9999.0f) lineBreakMode:UILineBreakModeWordWrap];
    // numberOfTextRows is an integer.

    numberOfTextRows = (round(theSize.height/14));

    if ((indexPath.row== 0) || (numberOfTextRows <2)) {
        return 44;
    }

    else {
        return theSize.height +18;
    }
}

注: 表示される NSLog には (null) が表示されます。

4

1 に答える 1

0

これが私の解決策です。必要に応じてカスタマイズします。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellText;
    CGRect screenBound = [[UIScreen mainScreen] bounds];
    CGSize screenSize = screenBound.size;
    CGFloat screenWidth = screenSize.width;
    CGFloat screenHeight = screenSize.height;

    if (searchDisplayController.active && [searchResults count]) {
        cellText = [searchResults objectAtIndex:indexPath.row];
    } else {
        cellText = [detailPeriodsContent objectAtIndex:indexPath.row];
    }
    UIFont *cellFont = [UIFont systemFontOfSize:14];
    CGSize constraintSize = CGSizeMake(screenWidth-40, MAXFLOAT);
    CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

    return labelSize.height + 35;
}
于 2013-03-03T17:34:59.817 に答える