2010-09-17 8 views
14

¿Es posible modificar la altura de una sola celda en una vista de tabla agrupada?
Tengo una vista de tabla con 2 secciones de 3 y 2 filas ... Cambiaría la altura de fila de la segunda fila de la segunda sección ...tableView cell height ... ¿cómo personalizarlo?

¿Cómo puedo hacer esto?
Gracias!

Respuesta

51

Usted puede mirar en este método:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

En su caso, el código debería verse así:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.section == 1 && indexPath.row == 1) { 
     return SPECIAL_HEIGHT; 
    } 
    return NORMAL_HEIGHT; 
} 

Usted puede mirar para más detalles acerca the method here

+0

Ok gracias! Pero tengo un problema ... si escribo un número como return 60.0; funciona mucho, pero si escribo algún código para devolver la altura en función de un texto UILabel, no aparece el fondo de la celda ... Agregué este código en el método cellForRowAtIndexPath: – matteodv

+0

else if (indexPath.section == 1 && indexPath.row == 1) { \t \t descrizioneLabel = [[UILabel alloc] initWithFrame: CGRectMake (10, 5, 285, 50)]; \t \t descrizioneLabel.font = [UIFont boldSystemFontOfSize: 14.0]; \t \t descrizioneLabel.backgroundColor = [UIColor clearColor]; \t \t descrizioneLabel.numberOfLines = 0; \t \t descrizioneLabel.text = wish.descrizione; \t \t [cell.contentView addSubview: descrizioneLabel]; \t} – matteodv

+0

y este es el método publicado por usted: - (CGFloat) tableView: (UITableView *) tableView heightForRowAtIndexPath: (NSIndexPath *) indexPath { \t \t si (indexPath.section == == 1 && indexPath.row 1) { \t \t NSString * test = descrizioneLabel.text; \t \t UIFont * cellFont = [UIFont boldSystemFontOfSize: 14.0]; \t \t CGSize constraintSize = CGSizeMake (280.0, MAXFLOAT); \t \t CGSize labelSize = [testo sizeWithFont: cellFont constrainedToSize: constraintSize lineBreakMode: UILineBreakModeWordWrap]; \t \t \t \t return labelSize.height; \t} \t \t return 44.0; } – matteodv

0

Tenga una mirada en

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 

Para el indexpath sólo echa de la fila y la sección y ajustar la altura consecuencia

3

En iOS 8 y arriba podemos usar Dynamic Table View Cell Height.

uso de esta función UITableviewCell obtener su altura de su contenido y no necesitamos escribir heightForRowAtIndexPath

Todo lo que tengo que hacer en viewDidLoad()

tableView.estimatedRowHeight = 44.0; 
tableView.rowHeight = UITableViewAutomaticDimension; 

Si cell contiene uilabel. a continuación, aplicar restricciones a uilabel

enter image description here

Asegúrese de cambiar la etiqueta --Lines-- a 0

enter image description here

La célula crecerá de forma automática con el contenido de uilabel:

enter image description here