iphone uitableview resize uitextview

iphone - Cómo redimensionar dinámicamente la altura de UITableViewCell



resize uitextview (4)

Tengo el clásico UITableView agrupado con UITextViews editable dentro de cada célula. Estas vistas de texto pueden tener una sola línea o múltiples líneas, y quiero que la celda aumente su altura a medida que el usuario escribe y el texto comienza una nueva línea.

Mi pregunta es: ¿necesito volver a cargar toda la tabla solo para aumentar la altura de una celda? ¿No hay otro método?

He estado buscando mucho, y las respuestas y tutoriales anteriores solo hablan sobre cómo calcular la altura del texto, cómo implementar heightForRowAtIndexPath ... cosas que ya sé. Mi preocupación es que, para lograr lo que deseo, tenga que calcular la altura y volver a cargar la tabla cada vez que el usuario ingrese un nuevo personaje, que no me parece muy limpio ni eficiente.

Gracias.


Para ser más específico, sí, tienes que implementar tableView:heightForRowAtIndexPath: para calcular la nueva altura, haz lo que dice rickharrison y llama a [tableView reloadRowsAtIndexPaths:withRowAnimation] . Digamos que sus celdas pueden tener una altura expandida y una altura normal, y usted desea que crezcan cuando se toca. Tu puedes hacer:

-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*) indexPath { if ([expandedPaths containsObject:indexPath]) { return 80; } else { return 44; } } -(void)tableView:(UITableView*) didSelectRowAtIndexPath:(NSIndexPath*) indexPath { [expandedPaths addObject:indexPath]; [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; }


-reloadRowsAtIndexPaths:withRowAnimation no redimensionó la altura de UITableViewCell, incluso después de que cambié el marco de la celda. Solo funcionó cuando lo seguí con -reloadData :

[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationFade]; [tableView reloadData];


No siempre tienes que volver a cargar toda la tabla. En su lugar, solo puedes volver a cargar esa fila.

[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];


[tableView beginUpdates]; [tableView endUpdates];