Attenzione: “Il destinatario del messaggio 'sizeWithFont: constrainedToSize: lineBreakMode:' è pari a zero ...”
-
24-10-2019 - |
Domanda
ho ottenuto questo avvertimento:
"Il destinatario del messaggio 'sizeWithFont: constrainedToSize: lineBreakMode:' è pari a zero e restituisce un valore di tipo 'CGSize' che sarà spazzatura"
Io non capisco. Che cosa sto facendo di sbagliato?
Ecco il codice che sto usando:
- (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;
}
Soluzione
La causa è il seguente segmento di codice:
if (indexPath.section == FIRST_SECTION) {
text = [_firstArray objectAtIndex:row];
} else if (indexPath.section == SECOND_SECTION) {
text = [_secondArray objectAtIndex:row];
} else {
NSLog(@"Wrong section");
}
In che nella parte altra cosa, nessun valore viene assegnato alla variabile di testo. Quindi, rimane come nullo. Così, XCode si lamenta che sia nullo.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow