va a responder a mi propia pregunta aquí:
Originalmente yo estaba bastante seguro de que los cálculos de altura fueron atados al método tableView:cellForRowAtIndexPath:
, y no se podían mover en otro lugar. Sin embargo, con un montón de reestructuraciones, pude sacar esas cosas de allí y entrar al tableView:heightForRowAtIndexPath:
, que resuelve todo.
Para cualquier otra persona que está tratando de obtener células de auto-ajustable en altura para trabajar, aquí hay un código que podría ayudar:
// Inside tableView:cellForRowAtIndexPath:
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = self.numberOfTextRows;
// numberOfTextRows is an integer, declared in the class
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGSize theSize = [theObject.theStringToDisplay sizeWithFont:[UIFont systemFontOfSize:18.0f] constrainedToSize:CGSizeMake(265.0f, 9999.0f) lineBreakMode:UILineBreakModeWordWrap];
// This gets the size of the rectangle needed to draw a multi-line string
self.numberOfTextRows = round(theSize.height/18);
// 18 is the size of the font used in the text label
// This will give us the number of lines in the multi-line string
if ((indexPath.section == FIXED_HEIGHT_SECTION) || (self.numberOfTextRows < 2)) {
return 44;
// 44 is the default row height; use it for empty or one-line cells (or in other table sections)
} else {
return theSize.height + 16;
// 16 seems to provide a decent space above/below; tweak to taste
}
}
Si usted puede pensar en una manera más precisa para calcular la altura apropiada de la célula, Soy todo oídos. :)
No estoy realmente seguro de por qué usted divide la altura rect por 18 y luego la multiplica de nuevo, pero aparte de eso lo hago de la misma manera. –
Bueno, la primera división es averiguar cuántas filas son necesarias, así que puedo establecer 'cell.textLabel.numberOfLines' correctamente; si no configuro eso, todo se imprime en una línea. Pero sí, creo que podría usar 'theSize.height' en el último bit allí, en lugar de' self.numberOfTextRows'. – Triz
Editado la respuesta para mayor claridad y simplificación. Funciona muy bien ahora. – Triz