2012-02-25 8 views
5

simplemente como fondo, esta pregunta se relaciona con éste he publicado anteriormente: Trying to expand/collapse UITableViewCell from a UIButton on the custom cellUITableViewCell Expand/Collapse Animación (Funciona cuando se expande, pero no colapsar)

En resumen, tengo una UITableView con células mesa varias personalizados . Cada UITableViewCell personalizada tiene un área de texto, seguida de un botón "Ver más". Esencialmente, cada celda mostrará inicialmente 3 líneas de texto, pero cuando el usuario toca el botón "Ver más", la celda debe expandirse a toda la altura del área de texto. Tocando el botón nuevamente colapsará la celda.

creo que esto es difícil de visualizar, por lo que han tenido un breve vídeo aquí: http://dl.dropbox.com/u/762437/cell-animation.mov

(se trata de un archivo de 3,5 MB, por lo que shouldnt se necesita tanto tiempo para cargar). En el video, muestro una celda haciendo la animación de expansión/colapso. Al hacer la expansión, el botón "Ver más" se anima hacia abajo. Al tocar nuevamente, simplemente salta a su posición original sin animación. Editar: Lo siento, debería ser más claro. El UITableView está animando las celdas correctamente, lo que estoy preguntando es cómo hacer que el botón "Ver más" se active correctamente.

Tengo el trabajo de expandir/colapsar (¡si lo hice bien o mal es otra cosa!), Pero las animaciones no funcionan como esperaba. La animación en expansión funciona, pero no el colapso.

En cada clase UITableViewCell costumbre, tengo una IBAction que se llama cuando el usuario pulsa la opción "Ver Más" botón. También hago un seguimiento de las celdas que están actualmente expandidas en un NSMutableArray. Cuando el usuario toca el botón "Cerrar", esa celda se elimina de la matriz.

En mi cellForRowAtIndexPath de tableView, puedo comprobar la matriz para ver qué células deben ampliarse y que debe mostrar en su tamaño predeterminado.

lo estoy haciendo con este código:

// Check if the array contains the particular cell, if so, then it needs to be expanded 
if([expandedRows containsObject:indexPath]) 
     { 
      // Expand the cell's text area to fit the entire contents 
      [cell.textLabel sizeToFitFixedWidth:cell.textLabel.frame.size.width]; 

      // Find the new Y-position of where the "Close" button. 
      // The new Y position should be at the bottom of the newly expanded text label 

      CGFloat bottomYPos = cell.textLabel.frame.origin.y + cell.textLabel.frame.size.height; 

      // Get a rect with the "Close" button's frame and new Y position 
      CGRect buttonRect = cell.showAllButton.frame; 
      buttonRect.origin.y = bottomYPos; 

      // Animation block to shift the "Close" button down to its new rect    
      [UIView animateWithDuration:0.3 
            delay:0.0 
           options: UIViewAnimationCurveLinear 
          animations:^{ 
           cell.showAllButton.frame = buttonRect; 
           [cell.showAllButton setTitle:@"Close" forState:UIControlStateNormal]; 
          } 
          completion:^(BOOL finished){ 
           NSLog(@"Done 1!"); 
          }]; 
     } 
     else 
     { 
      // This cell is currently collapsed 

      // Get the button rect and subtract the height delta to put it back 
      // OriginalCellSizes is where I keep track of the original Y positions 
      CGRect buttonRect = cell.showAllButton.frame; 
      buttonRect.origin.y -= cell.frame.size.height - [[originalCellSizes objectAtIndex:indexPath.row] floatValue]; 

      // Animation block for the Button  
      [UIView animateWithDuration:0.3 
            delay:0.0 
           options: UIViewAnimationCurveEaseOut 
          animations:^{ 
           cell.showAllButton.frame = buttonRect; 
           [cell.showAllButton setTitle:@"View More" forState:UIControlStateNormal]; 
          } 
          completion:^(BOOL finished){ 
           NSLog(@"Done! 2"); 
          }]; 
     } 

Al investigar, descubrí que en la rama "si no" de que si-else, la buttonRect ya está en la misma posición Y como la posición original . Sospecho que es por eso que no hay animación, pero no estoy seguro.

Cualquier ayuda sería realmente genial!

Respuesta

-1

forma sencilla de hacer esto ......

[self.tableView beginUpdates]; 
[self.tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationFade]; 
[self.tableView deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade]; 
[self.tableView endUpdates]; 

insertIndexPaths es una matriz de NSIndexPaths para ser insertado a su mesa.

deleteIndexPaths es un conjunto de NSIndexPaths que desea eliminar de su mesa.

Ejemplo formato de matriz de caminos de índice:

NSArray *insertIndexPaths = [[NSArray alloc] initWithObjects: 
     [NSIndexPath indexPathForRow:0 inSection:0], 
     [NSIndexPath indexPathForRow:1 inSection:0], 
     [NSIndexPath indexPathForRow:2 inSection:0], 
     nil]; 

lo obtuvieron de esta pregunta ... this question...

+1

Esta respuesta no tiene nada que ver con la pregunta que se hizo. – Dalmazio

+0

¿Puedes ver que se acepta la respuesta? –

+2

Sí, y eso es aún más desconcertante. – Dalmazio

Cuestiones relacionadas