2012-02-10 14 views
10

El que ve a continuación es una captura de pantalla de Tweetbot iPhone application. Lo que quiero preguntar es si sabes cómo puedo lograr lo que los chicos de Tapbots lograron en su tableView: tocas una celda de la antigua tableView y revela un conjunto de acciones, toca de nuevo y desaparece.TableView personalizado en el Tweetbot

enter image description here

Es una aplicación muy cool mirando y me gustaría saber su opinión sobre cómo puedo hacer eso, tanto para la práctica y también porque me gustaría utilizar una variante de este en un proyecto de futuro.

¿Alguna idea?

+0

Creo que en el método 'tableView: didSelectRowAtIndexPath' simplemente recarga la fila en la ruta de índice seleccionada. Y al tocar en una celda personalizada, se hace más grande o algo así. –

Respuesta

24

Cuando el usuario selecciona la celda, guarde la ruta de índice de la celda seleccionada y agregue una fila debajo de ella. Si el usuario selecciona la misma venta, oculte la fila debajo de esta. Si el usuario selecciona otra celda, oculte la celda actualmente seleccionada y muestre una nueva celda debajo de la celda recién seleccionada.

@interface RCTableViewController() 

@property (nonatomic, strong) NSMutableArray *tableViewData; 
@property (nonatomic, strong) NSIndexPath *selectedIndexPath; 

@end 

@implementation RCTableViewController 

@synthesize tableViewData = _tableViewData; 
@synthesize selectedIndexPath = _selectedIndexPath; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.tableViewData = [NSMutableArray arrayWithObjects: 
       @"Cell 0", 
       @"Cell 1", 
       @"Cell 2", 
       @"Cell 3", 
       @"Cell 4", 
       @"Cell 5", 
       @"Cell 6", 
       @"Cell 7", 
       @"Cell 8", 
       nil]; 
    self.selectedIndexPath = nil; 
    [self.tableView reloadData]; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return self.tableViewData.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell; 

    if (indexPath == self.selectedIndexPath) { 
     static NSString *DropDownCellIdentifier = @"DropDownCell"; 

     cell = [tableView dequeueReusableCellWithIdentifier:DropDownCellIdentifier]; 
     if (cell == nil) { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:DropDownCellIdentifier]; 
     } 

     cell.textLabel.text = @"Drop Down Cell"; 

    } else { 
     static NSString *DataCellIdentifier = @"DataCell"; 

     cell = [tableView dequeueReusableCellWithIdentifier:DataCellIdentifier]; 
     if (cell == nil) { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:DataCellIdentifier]; 
     } 

     cell.textLabel.text = [self.tableViewData objectAtIndex:indexPath.row]; 
    } 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSIndexPath *dropDownCellIndexPath = [NSIndexPath indexPathForRow:indexPath.row + 1 
                  inSection:indexPath.section]; 

    if (!self.selectedIndexPath) { 
     // Show Drop Down Cell   
     [self.tableViewData insertObject:@"Drop Down Cell" atIndex:dropDownCellIndexPath.row]; 

     [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:dropDownCellIndexPath] 
         withRowAnimation:UITableViewRowAnimationTop];   

     self.selectedIndexPath = indexPath;  
    } else { 
     NSIndexPath *currentdropDownCellIndexPath = [NSIndexPath indexPathForRow:self.selectedIndexPath.row + 1 
                   inSection:self.selectedIndexPath.section]; 

     if (indexPath.row == self.selectedIndexPath.row) { 
      // Hide Drop Down Cell 
      [self.tableViewData removeObjectAtIndex:currentdropDownCellIndexPath.row]; 

      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:currentdropDownCellIndexPath] 
          withRowAnimation:UITableViewRowAnimationTop]; 

      self.selectedIndexPath = nil; 
     } else if (indexPath.row == currentdropDownCellIndexPath.row) { 
      // Dropdown Cell Selected - No Action 
      return; 
     } else { 
      // Switch Dropdown Cell Location  
      [tableView beginUpdates]; 
      // Hide Dropdown Cell 
      [self.tableViewData removeObjectAtIndex:currentdropDownCellIndexPath.row]; 

      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:currentdropDownCellIndexPath] 
          withRowAnimation:UITableViewRowAnimationAutomatic]; 

      // Show Dropdown Cell    
      NSInteger dropDownCellRow = indexPath.row + ((indexPath.row >= currentdropDownCellIndexPath.row) ? 0 : 1); 
      dropDownCellIndexPath = [NSIndexPath indexPathForRow:dropDownCellRow 
                 inSection:indexPath.section]; 


      [self.tableViewData insertObject:@"Drop Down Cell" atIndex:dropDownCellIndexPath.row]; 

      [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:dropDownCellIndexPath] 
          withRowAnimation:UITableViewRowAnimationAutomatic];   

      self.selectedIndexPath = [NSIndexPath indexPathForRow:dropDownCellIndexPath.row - 1 
                 inSection:dropDownCellIndexPath.section]; 
      [tableView endUpdates];        
     }   
    } 
} 

@end 
+0

buen consejo, gracias. – Francesco

+0

¡Gracias por el código agradable y muy útil! – Lindemann

Cuestiones relacionadas