2009-12-30 3 views
1

J'ai une application très simple avec un UITableViewController. Lors de l'EDITION, j'essaie de glisser une rangée en position 0 de la première section. La nouvelle ligne doit avoir un style d'édition INSERT tandis que la ligne existante doit avoir un style DELETE.Comment utiliser tableView: editingStyleForRowAtIndexPath?

J'ai surchargé les 4 méthodes suivantes:


// Customize the number of rows in the table view. 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if (self.editing && section == 0) { 
return2; 
    } 
    return 1; 
} 

- (UITableViewCellEditingStyle)tableView:(UITableView*)tableView 
      editingStyleForRowAtIndexPath:(NSIndexPath*)indexPath { 
    int section = indexPath.section; 
    int row = indexPath.row; 
    if (self.editing && section == 0 && row == 0) { 
return UITableViewCellEditingStyleInsert; 
    } 
return UITableViewCellEditingStyleDelete; 
} 

- (void)setEditing:(BOOL)editing animated:(BOOL)animated { 
    [super setEditing:editing animated:animated]; 
NSIndexPath *ip = [NSIndexPath indexPathForRow:0 inSection:0]; 
    [self.tableView beginUpdates]; 
    if (editing) { 
      [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:ip] 
withRowAnimation:UITableViewRowAnimationLeft]; 
    } else { 
      [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:ip] 
withRowAnimation:UITableViewRowAnimationFade];   
    } 
    [self.tableView endUpdates]; 
} 

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"ClientsControllerCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc ] initWithStyle:UITableViewCellStyleValue1reuseIdentifier:CellIdentifier] autorelease]; 
    } 
    int section = indexPath.section; 
    int row = indexPath.row; 
    if (self.editing && section == 0 && row == 0) { 
      cell.textLabel.text = @"Add Me"; 
      cell.detailTextLabel.text = @"Detail text";   
    } else { 
      cell.textLabel.text = @"Test me"; 
      cell.detailTextLabel.text = @"Detail text";   
    } 
    return cell; 
} 

Mais dès que je passe en mode EDIT, « les deux » cellules finissent avec un style d'édition de UITableViewCellEditingStyleInsert.

Si je change ma logique et ajoute la nouvelle cellule à la FIN - alors il dessine correctement les cellules avec un style DELETE et la nouvelle cellule obtient un INSERT.

De toute façon, tableView: editingStyleForRowAtIndexPath est invoqué 4 fois. En fait, si j'insère la nouvelle cellule dans la section: 0 row: 0, cette méthode est appelée avec la section: row 0: 0, 0: 1, 0: 0, 0: 0. Alors que si j'ajoute la nouvelle cellule dans la section: row: 1, cette méthode est appelée avec la section: row 0: 0, 0: 1, 0: 0, 0: 1.

Qu'est-ce qui me manque? Je devrais pouvoir insérer une rangée et l'attraper correctement? Pour une raison quelconque, je ne peux pas voir section = 0 row = 1 à travers une seconde fois.

-Luther

Répondre

1

Il y a une autre question sur StackOverflow qui semble poser essentiellement la même chose: SO 1508066.

La réponse indique qu'il n'est pas standard de placer la ligne Insert en haut; il devrait aller au fond à la place. Je ne suis pas sûr d'être d'accord avec cette affirmation, mais c'est certainement la voie de la moindre résistance.

Questions connexes