一次只允许一个UITableViewCellAccessoryCheckmark

时间:2011-04-15 13:07:25

标签: iphone cocoa-touch uitableview

我有一个tableview,其中包含一个用户可以“选择”的声音列表。只有当前选定的声音应显示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];



    }

}

2 个答案:

答案 0 :(得分:4)

您不记得为出列的单元格重置附件类型。

这应该为你解决;

  cell.accessoryType = UITableViewCellAccessoryNone;

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

方便提示:您可能会发现使用switch(indexPath.section)而不是很多if ... elseif ... else结构很有用。虽然你现在只用了2个部分就可以了,但我发现使用switch可以更容易地扩展部分的数量,并且通过代码可以更容易地阅读,因为你经常使用if .. else构造用于其他事情 - 具有不同的语法有助于使测试更加清晰。

答案 1 :(得分:1)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath的每次声明之后的cell中,尝试通过添加以下行重置单元accessoryType:cell.accessoryType = UITableViewCellAccessoryNone;