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 回答