2011-03-15 38 views
0

我得到了这样的警告:警告: “消息的接收者 'sizeWithFont:constrainedToSize:lineBreakMode:' 是零......”

“消息的接收者 'sizeWithFont:constrainedToSize:lineBreakMode:' 是零并返回类型'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

原因是下面的代码段:

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

在,在其他部分,没有值分配给文本变量。所以,它仍然是零。所以,XCode抱怨它是空的。

+0

即使给“text”变量添加一个值,警告仍然存在......(虽然现在nil警告消失了)问题不是文本变量,它是CGSize,它会让我困扰的“垃圾”大多数...... – treasure 2011-03-15 06:50:44

+0

您是否尝试在更改后再次构建代码?你也可以请发布更新的代码? – Krishna 2011-03-15 06:53:12

+0

更新:我添加其他{ text = nil; } – treasure 2011-03-15 06:55:46