TableView和heightForRowAtIndexPath

时间:2012-10-02 15:36:08

标签: objective-c ios xcode height tableview

我希望每个帖子在多行中显示完整标题,每个单元格根据文本标题字符编号拥有自己的自定义高度。我做了这个,但它不起作用。我认为应该独立调用每个对象。我该怎么做才能做到这一点?这是我的代码:

- (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) 提前谢谢你..

1 个答案:

答案 0 :(得分: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;
}