2011-04-15 175 views
3

我有一個tableview,其中包含用戶可以「挑選」的聲音列表。只有當前選擇的聲音應顯示UITableViewCellAccessoryCheckmark。基本上我得到它的工作。但是,例如,檢查底部單元格,然後向上滾動表格視圖(使選中的單元格離開視圖),然後檢查另一個單元格,底部(視圖外)的單元格不會被刪除。有人知道爲什麼嗎?一次只允許有一個UITableViewCellAccessoryCheckmark

- (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; 

     }  
    } 

和:

- (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]; 



    } 

} 

回答

4

你沒記重設離隊細胞的附件類型。

這應該爲您解決它;

cell.accessoryType = UITableViewCellAccessoryNone; 

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

方便的技巧:你可能會發現,使用開關(indexPath.section),而不是很多,如果... ... ELSEIF其他構造。雖然你現在只用了2個部分,但我發現使用switch可以更容易地擴展部分的數量,並且使得通過代碼閱讀更容易,因爲你經常有if .. else構造用於其他事情 - 具有不同的語法有助於使測試更清晰。

+0

我同意,usinga開關stament是非常有用的,更易於讀取,這種情況下。 – Sabobin 2011-04-15 13:33:22

+0

太棒了。非常感謝 – 2011-04-15 13:36:05

1

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPathcell每個聲明嘗試正在重置通過添加以下行accessoryType細胞:cell.accessoryType = UITableViewCellAccessoryNone;