2011-04-15 3 views
3

J'ai une tableview avec une section qui contient une liste de sons que l'utilisateur peut "choisir". Seul le son actuellement sélectionné doit afficher un UITableViewCellAccessoryCheckmark. Fondamentalement, je l'ai fonctionné. Cependant, lorsque, par exemple, la cellule inférieure est vérifiée, et je fais défiler la vue de la table (rendant la cellule cochée hors de vue), et je vérifie une autre cellule, la vérification de la cellule inférieure (hors de vue) n'est pas supprimée. Est-ce que quelqu'un sait pourquoi c'est comme ça?Un seul UITableViewCellAccessoryCheckmark autorisé à la fois

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    static NSString *TheOtherOne = @"OtherCell"; 
    static NSString *MiddleCell = @"MiddleCell"; 



    // Configure the cell... 

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 
    NSString *theSound = [prefs objectForKey:@"pickedFailSound"]; 
    NSUInteger index = [failSounds indexOfObject:theSound]; 


    if (indexPath.section == 0){ 

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


     failAtLaunch = [[[UISwitch alloc] initWithFrame:CGRectMake(200, 7, 100, 30)] autorelease]; 
     [cell addSubview:failAtLaunch]; 
     cell.accessoryView = failAtLaunch; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 

     NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 

     if(![prefs boolForKey:@"failAtLaunch"]) { 
      [failAtLaunch setOn:NO animated:NO]; 
     } else { 
      [failAtLaunch setOn:YES animated:NO]; 
     } 

     [(UISwitch *)cell.accessoryView addTarget:self action:@selector(setIt) forControlEvents:UIControlEventValueChanged]; 
     cell.textLabel.text = @"Instafail"; 
     cell.detailTextLabel.text = @"Fail at Laucnh"; 

     return cell; 

    } else if (indexPath.section == 1) { 

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

     NSString *cellTitle = [failSounds objectAtIndex:indexPath.row]; 
     cell.textLabel.text = cellTitle; 

     if (indexPath.row == index) { 
      cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     } 



     if ([cellTitle isEqualToString:@"Your Fail Sound"]){ 

      NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 
      if(![prefs boolForKey:@"customSoundAvailable"]) { 

       cell.selectionStyle = UITableViewCellSelectionStyleNone; 
       cell.userInteractionEnabled = NO; 
       cell.textLabel.textColor = [UIColor grayColor]; 
       cell.detailTextLabel.text = @"Record a Custom Failsound First"; 
      } 
     } 

    return cell; 

    } else { 

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TheOtherOne]; 
     if (cell == nil) { 
      cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:TheOtherOne] autorelease]; 
     } 


     cell.textLabel.text = @"Hold to record fail sound"; 
     UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     btn.frame = CGRectMake(230.0f, 4.0f, 60.0f, 36.0f); 
     [btn setTitle:@"OK" forState:UIControlStateNormal]; 
     [btn setTitle:@"OK" forState:UIControlStateSelected]; 
     [btn addTarget:self action:@selector(recordAudio) forControlEvents:UIControlEventTouchDown]; 
     [btn addTarget:self action:@selector(stop) forControlEvents:UIControlEventTouchUpInside]; 
     [cell.contentView addSubview:btn]; 


     return cell; 

     }  
    } 

Et:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 


    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 
    NSString *theSound = [prefs objectForKey:@"pickedFailSound"]; 
    NSUInteger index = [failSounds indexOfObject:theSound]; 

    //NSLog(@"The index is: %@", index); 

    if (indexPath.section == 1){ 


       NSIndexPath *oldIndexPath = [NSIndexPath indexPathForRow:index inSection:1]; 

       NSLog(@"Oldindexpath is: %@", oldIndexPath); 
       UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath]; 


       if (newCell.accessoryType == UITableViewCellAccessoryNone) { 

        newCell.accessoryType = UITableViewCellAccessoryCheckmark; 
       } 

       UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:oldIndexPath]; 


       if (oldCell != newCell){ 

        if (oldCell.accessoryType == UITableViewCellAccessoryCheckmark) { 

         oldCell.accessoryType = UITableViewCellAccessoryNone; 
        } 
       } 



    NSString *pickedSound = [failSounds objectAtIndex:indexPath.row]; 
    NSLog(@"Picked sound is %@", pickedSound); 
    [prefs setObject:pickedSound forKey:@"pickedFailSound"]; 
    [prefs synchronize];   

    [self performSelector:@selector(loadSound) withObject:nil]; 
    [failSound play]; 



    } 

} 

Répondre

4

Vous n'êtes pas ne pas oublier de réinitialiser le type d'accessoire pour les cellules dequeued.

Cela devrait résoudre le problème pour vous;

cell.accessoryType = UITableViewCellAccessoryNone; 

    if (indexPath.row == index) { 
      cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } 

Conseil pratique: Vous pouvez trouver utile d'utiliser l'interrupteur (indexPath.section) plutôt que beaucoup de si ... elseif ... else. Bien que vous soyez OK maintenant avec seulement 2 sections, je trouve que l'utilisation du commutateur facilite l'augmentation du nombre de sections et rend aussi mentalement plus facile la lecture du code puisque vous avez souvent si ... d'autres constructions utilisées pour d'autres choses - Avoir la syntaxe différente aide à clarifier la raison d'être des tests.

+0

Je suis d'accord, usinga stament de commutation est très utile et plus facile à lire pour ce cas. – Sabobin

+0

Génial. Merci beaucoup –

1

Dans - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath après chaque déclaration de cell essayer reseting les cellules accessoryType en ajoutant la ligne suivante: cell.accessoryType = UITableViewCellAccessoryNone;

Questions connexes