2012-02-10 5 views
10

Quella che vedi qui sotto è una schermata di Tweetbot iPhone application. Quello che voglio chiedere è se conosci un modo per raggiungere quello che i ragazzi di Tapbots hanno ottenuto nella loro tableView: tocchi una cella del precedente tableView e rivela un insieme di azioni, tocca di nuovo e scompare.Tabella personalizzataVisualizza à Tweetbot

enter image description here

Si tratta di un'implementazione davvero cool cercando e mi piacerebbe conoscere la vostra opinione su come posso farlo sia per la pratica e anche perché mi piacerebbe utilizzare una variante di questo in un progetto futuro.

Qualche idea?

+0

Penso che nel metodo tableView: didSelectRowAtIndexPath' si ricarichi semplicemente la riga al percorso dell'indice selezionato. E alla spina nella cella personalizzata, ingrandisce o qualcosa del genere. –

risposta

24

Quando l'utente seleziona la cella, salva il percorso dell'indice della cella selezionata e aggiunge una riga al di sotto di essa. Se l'utente seleziona la stessa vendita, nasconde la riga sottostante. Se l'utente seleziona un'altra cella nasconde la cella attualmente selezionata e mostra una nuova cella sotto la cella appena selezionata.

@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

bel consiglio, grazie. – Francesco

+0

Grazie per il codice bello e molto utile! – Lindemann