0

我收到了这个警告:

“消息‘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;

}
4

1 回答 1

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 部分中,没有为 text 变量分配任何值。因此,它仍然为零。因此,XCode 抱怨它为空。

于 2011-03-15T06:43:06.597 回答