2010-07-16 2 views
0

J'essaie de réaliser un UITableViewController avec 7 sections. Chaque section a un nombre de lignes qui peuvent changer par les événements de l'utilisateur. Par exemple, dans la première section, j'ai une seule ligne avec un UISwitch: si l'utilisateur active le commutateur, 2 lignes supplémentaires apparaissent dans cette section. La même chose pour la section 2. Ok pour l'instant .... Dans les sections 3,4,5 j'ai aussi une seule ligne (Value1 Cell, les paramètres généraux de l'iPhone comme): en appuyant sur cette cellule, les sections apparaissent sur la 2ème rangée un contrôle UISegmented avec 3 segments. En appuyant sur un de ces segments, l'utilisateur fait son choix et la ligne de contrôle segmentée disparaît, les sections ont à nouveau une seule ligne avec la valeur choisie comme detailTextLabel.iPhone UITableViewController affiche/cache des lignes pendant l'exécution

Eh bien, toutes ces procédures sont gérées avec une méthode reloadSections qui recharge les sections lorsque l'utilisateur interagit. Mon code est ci-dessous:

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

UITableViewCell *cell = [ tableView dequeueReusableCellWithIdentifier: CellIdentifier]; 
CellIdentifier = [ NSString stringWithFormat: @"%d:%d", [ indexPath indexAtPosition: 0 ], [ indexPath indexAtPosition:1 ]]; 

//cell = nil; 
if(indexPath.section == 0 && [indexPath indexAtPosition: 1] == 0) 
{ 
    CellIdentifier = @"test"; 
} 


if (cell == nil) { 
    cell = [ [ [ UITableViewCell alloc ] initWithFrame: CGRectZero reuseIdentifier: CellIdentifier] autorelease ]; 

    cell.selectionStyle = UITableViewCellSelectionStyleNone; 

    switch ([ indexPath indexAtPosition: 0]) { 
     case(0): { 
      switch([ indexPath indexAtPosition: 1]) { 
       case(0): 
       { 
        positionControl.tag = 0; 
        [positionControl addTarget:self action:@selector(toggleSwitch:) forControlEvents:UIControlEventValueChanged]; 
        [ cell addSubview: positionControl ]; 
        cell.textLabel.text = NSLocalizedString(@"Position",@""); 

       } 
        break; 
       case(1): 
       { 
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
        cell.textLabel.text = NSLocalizedString(@"Some Position",@""); 
        cell.detailTextLabel.text = NSLocalizedString(@"Last Known Position",@""); 
       } 
        break; 
       case(2): 
       { 
        cell.textLabel.text = NSLocalizedString(@"ReDo Position",@""); 
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
       } 
        break; 
      } 
     } 
      break; 
     case(1): { 
      switch ([ indexPath indexAtPosition: 1 ]) { 
       case(0): 
       { 
        photoControl.tag = 1; 
        [photoControl addTarget:self action:@selector(toggleSwitch:) forControlEvents:UIControlEventValueChanged]; 
        [ cell addSubview: photoControl ]; 
        cell.textLabel.text = NSLocalizedString(@"photos",@""); 
       } 
        break; 
       case(1): 
       { 
        cell.textLabel.text = NSLocalizedString(@"Take photo",@""); 
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
       } 
        break; 
       case(2): 
       { 
        cell.textLabel.text = NSLocalizedString(@"Select from Roll",@""); 
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
       } 
        break; 
      } 
     } 
      break; 
     case(2): { 
      switch ([ indexPath indexAtPosition: 1 ]) { 
       case(0): 
       { 

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease]; 
        cell.textLabel.text = NSLocalizedString(@"size",@""); 
        if (UISegmentedControlNoSegment != [dimensionSegmentedControl selectedSegmentIndex]) { 
         cell.detailTextLabel.text = [dimensionSegmentedControl titleForSegmentAtIndex:[dimensionSegmentedControl selectedSegmentIndex]]; 
        } 
        else { 
         cell.detailTextLabel.text = NSLocalizedString(@"Select",@""); 
        } 
       } 
        break; 
       case(1): 
       { 
        [dimensionSegmentedControl addTarget:self action:@selector(onSegmentedControlChanged:) forControlEvents:UIControlEventValueChanged]; 
        [cell.contentView addSubview:dimensionSegmentedControl]; 
       } 
        break; 
      } 
     } 
      break; ... 

Ensuite, mon action pour les interrupteurs:

- (IBAction) toggleSwitch: (id) sender { 
UISwitch *switchControl = (UISwitch *) sender; 
NSUInteger tag = switchControl.tag; 
NSIndexSet *sectionsToReload = [[NSIndexSet alloc] initWithIndex:tag]; 
[self.tableView reloadSections:sectionsToReload withRowAnimation:NO]; 
[sectionsToReload release];} 

action pour segmentedcontrol

- (IBAction) onSegmentedControlChanged: (id) sender { 
UISegmentedControl *segmentedControl = (UISegmentedControl *) sender; 
NSUInteger tag = segmentedControl.tag; 
NSIndexSet *sectionsToReload = [[NSIndexSet alloc] initWithIndex:tag]; 
segmentedControl.momentary = YES; 
[self.tableView reloadSections:sectionsToReload withRowAnimation:NO]; 
segmentedControl.momentary = NO; 
[sectionsToReload release];} 

C'est le didSelectRow

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
switch ([ indexPath indexAtPosition: 0]) {  
    case(2): { 
     switch([ indexPath indexAtPosition: 1]) { 
      case (0): { 
       if (UISegmentedControlNoSegment != [dimensionSegmentedControl selectedSegmentIndex]) { 
        dimensionSegmentedControl.selectedSegmentIndex = [dimensionSegmentedControl selectedSegmentIndex]; 
       } 
       else { 
        dimensionSegmentedControl.selectedSegmentIndex = 0; 
       } 
       NSIndexSet *sectionsToReload = [[NSIndexSet alloc] initWithIndex:2]; 
       //[self.tableView reloadData]; 
       [self.tableView reloadSections:sectionsToReload withRowAnimation:NO]; 
       [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
       [sectionsToReload release]; 
      } 
       break; 
     } 
    } 
     break; 
    case(3): { 
     switch([ indexPath indexAtPosition: 1]) { 
      case (0): { 
       if (UISegmentedControlNoSegment != [patientsSegmentedControl selectedSegmentIndex]) { 
        patientsSegmentedControl.selectedSegmentIndex = [patientsSegmentedControl selectedSegmentIndex]; 
       } 
       else { 
        patientsSegmentedControl.selectedSegmentIndex = 0; 
       } 
       NSIndexSet *sectionsToReload = [[NSIndexSet alloc] initWithIndex:3]; 
       //[self.tableView reloadData]; 
       [self.tableView reloadSections:sectionsToReload withRowAnimation:NO]; 
       [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
       [sectionsToReload release]; 
      } 
       break; 
      default: 
       break; 
     } 
    } 
     break; 
    case(4): { 
     switch([ indexPath indexAtPosition: 1]) { 
      case (0): { 
       if (UISegmentedControlNoSegment != [patientsStateSegmentedControl selectedSegmentIndex]) { 
        patientsStateSegmentedControl.selectedSegmentIndex = [patientsStateSegmentedControl selectedSegmentIndex]; 
       } 
       else { 
        patientsStateSegmentedControl.selectedSegmentIndex = 0; 
       } 
       NSIndexSet *sectionsToReload = [[NSIndexSet alloc] initWithIndex:4]; 
       //[self.tableView reloadData]; 
       [self.tableView reloadSections:sectionsToReload withRowAnimation:NO]; 
       [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
       [sectionsToReload release]; 
      } 
       break; 
     } 
    } 
     break; 
    case(5): { 
     switch([ indexPath indexAtPosition: 1]) { 
       case (0): 
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://4030"]]; 
       break; 
       case (1): 
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://4030"]]; 
       break; 
     } 
    } 
     break; 
    default: break; 
}} 

Et enfin mes numbersOfRowsInSections , probablement t il problèmes source:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
// Return the number of rows in the section. 
switch (section) { 
    case (0): 
    { 
     if (!positionControl.on) { 
      return 1; 
      break; 
     } 
     return 3; 
    } 
     break; 
    case (1): 
    { 
     if (!photoControl.on) { 
      return 1; 
      break; 
     } 
     return 3; 
    } 
     break; 
    case (2): 
    { 
     if (UISegmentedControlNoSegment == [dimensionSegmentedControl selectedSegmentIndex] || [dimensionSegmentedControl isMomentary]) { 
      return 1; 
      break; 
     } 
     return 2; 
    } 
     break; 
    case (3): 
    { 
     if (UISegmentedControlNoSegment == [patientsSegmentedControl selectedSegmentIndex] || [patientsSegmentedControl isMomentary]) { 
      return 1; 
      break; 
     } 
     return 2; 
    } 
     break; 
    case (4): 
    { 
     if (UISegmentedControlNoSegment == [patientsStateSegmentedControl selectedSegmentIndex] || [patientsStateSegmentedControl isMomentary]) { 
      return 1; 
      break; 
     } 
     return 2; 
    } 
     break;  
    case (5): { 
     return 2; 
    } 
     break; 
    case (6): { 
     return 1; 
    } 
     break; 
} 
return 0;} 

Eh bien, maintenant vous pouvez voir mon code, je vais expliquer mon problème. Quand je fais face à UISwitches, les sections rechargent correctement .... les lignes apparaissent et disparaissent dans les deux premières sections comme un charme. Mais, lorsque je clique sur les cellules des sections avec des contrôles segmentés, la première fois tout va bien, la cellule montre le contrôle segmenté, l'utilisateur fait son choix, le contrôle segmenté disparaît et la ligne unique affiche le choix de l'utilisateur dans detailTextLabel. mais lorsque je tente de cliquer sur une autre ligne, les application se bloque avec cette exception:

*** en raison de l'application Mettre fin exception uncaught « NSInternalInconsistencyException », motif: « mise à jour invalide: numéro incorrect de lignes dans la section 3. Le nombre de lignes contenues dans une section existante après la mise à jour (2) doit être égal au nombre de lignes contenues dans cette section avant la mise à jour (1), plus ou moins le nombre de lignes insérées ou supprimées de cette section (0 , 0 supprimé). ' 16/07/2010 13: 09: 04,595 codes barres [40421: 207] Stack: ( 35943515, 2535093513, 36027451, 1928964 , 4228395, 4172781 , 278302, 4188742, 4171908 , 1425834, 35728064, 35724360 , 43226645, 43226842 , 3915695, 86847,)

La différence entre deux sections est que celles avec UISwitches affichent et masquent les lignes en activant/désactivant le commutateur, d'autres en sélectionnant les lignes (didSelectRow ....).

TOUT AIDE?

Merci d'avance.

+0

le deuxième paramètre de '-reloadSections: withRowAnimation:' n'est pas un 'BOOL'; c'est un type 'UITableViewRowAnimation' – user102008

Répondre

0

mon code est bon si j'ajoute simplement un drapeau (BOOL) est dit chaque commande segmentée doit être visible ou non. Régler sur "OUI" et "NON" ces drapeaux et en vérifiant leur valeur tout fonctionne correctement.

+0

YOu devrait marquer ceci comme répondu alors :) –

Questions connexes