警告:“消息的接收者'sizeWithFont:constrainedToSize:lineBreakMode:'是nil ......”

时间:2011-03-15 06:31:28

标签: iphone objective-c uitableview heightforrowatindexpath

我收到了这个警告:

  

“消息的接收者'sizeWithFont:constrainedToSize:lineBreakMode:'是nil并返回一个类型'CGSize'的值,它将是垃圾”

我不明白。我做错了什么?

这是我正在使用的代码:

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

        NSString *text = nil;
        NSUInteger row = indexPath.row;

    if (indexPath.section == FIRST_SECTION) {
        text = [_firstArray objectAtIndex:row];
    } else if (indexPath.section == SECOND_SECTION) {
        text = [_secondArray objectAtIndex:row];
    } else {
           text = nil;
        NSLog(@"Wrong section");
    }

    UITableViewCell *cell = [self myCell];
    UILineBreakMode lineBreakMode = cell.textLabel.lineBreakMode;

    CGFloat width = _tableView.contentSize.width - (kTableCellHPadding*2 + tableCellMargin*2);
    UIFont* font = cell.textLabel.font;
    CGSize size = [text sizeWithFont:font
                   constrainedToSize:CGSizeMake(width, CGFLOAT_MAX)
                       lineBreakMode:lineBreakMode];

    if (size.height > kMaxLabelHeight) size.height = kMaxLabelHeight;

    return size.height + kTableCellVPadding*2;

}

1 个答案:

答案 0 :(得分:1)

原因是以下代码段:

if (indexPath.section == FIRST_SECTION) {
    text = [_firstArray objectAtIndex:row];
} else if (indexPath.section == SECOND_SECTION) {
    text = [_secondArray objectAtIndex:row];
} else {
    NSLog(@"Wrong section");
}

在else部分中,没有为文本变量赋值。所以,它仍然是零。所以,XCode抱怨它是null。