¿Cómo puedo crear un UITableViewCell de tamaño variable?
-
22-07-2019 - |
Pregunta
Parece que no puedo hacer que el texto realmente abarque varias líneas. Las alturas se ven correctas. ¿Qué me estoy perdiendo?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"StatusCell"] autorelease];
CGRect frame = cell.contentView.bounds;
UILabel *myLabel = [[UILabel alloc] initWithFrame:frame];
myLabel.text = [[person.updates objectAtIndex:indexPath.row] valueForKey:@"text"];
[cell.contentView addSubview:myLabel];
[myLabel release];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *text = [[person.updates objectAtIndex:indexPath.row] valueForKey:@"text"];
UIFont *font = [UIFont systemFontOfSize:[UIFont systemFontSize]];
CGSize withinSize = CGSizeMake(tableView.frame.size.width, 1000);
CGSize size = [text sizeWithFont:font constrainedToSize:withinSize lineBreakMode:UILineBreakModeWordWrap];
return size.height + 20;
}
Además, ¿qué me falta para que las etiquetas aparezcan más largas que la celda de la tabla? ??
Solución
Tweetero
proporciona un ejemplo de esto en MessageListController.m
. El código allí muestra la siguiente pantalla:
(La foto está tomada de Mashable ).
El esquema básico de implementación:
-
Al construir un
UITableViewCell
, cree y agregue unUILabel
como una subvista de la manera que se muestra entableviewCellWithReuseIdentifier:
. Busque la creación de la etiquetaTEXT_TAG
. -
al enriquecer el
UITableViewCell
con vistas, asegúrese de formatear la etiqueta correctamente, como se hace enconfigureCell: forIndexPath
, busque de manera similar la etiqueta etiquetaTEXT_TAG . -
Devuelve la altura adecuada para cada celda, como se hace en
tableView:heightForRowAtIndexPath
.
Otros consejos
myLabel.numberOfLines = 2;
Verifique documentos para obtener información completa sobre cómo usar esta propiedad.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return theSizeYouWantYourCellToBe;
}