一次只允许一个UITableViewCellAccessoryCheckmark [英] Only one UITableViewCellAccessoryCheckmark allowed at a time

查看:83
本文介绍了一次只允许一个UITableViewCellAccessoryCheckmark的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含用户可以选择的声音列表的表格。只有当前选择的声音应显示 UITableViewCellAccessoryCheckmark 。基本上我得到它的工作。然而,检查底部单元格,并向上滚动表视图(使检查单元格不在视图中),并检查另一个单元格,底部(视图外)单元格的检查未删除。有没有人知道为什么是这样?

I have a tableview with a section that contains a list of sounds the user can "pick." Only the currently selected sound should show a UITableViewCellAccessoryCheckmark. Basically I got it working. However, when e.g. the bottom cell is checked, and I scroll up the table view (making the checked cell go out of view), and I check another cell, the check for the bottom (out of view) cell is not removed. Does anyone know why this is?

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



    }

}


推荐答案

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

You aren't remembering to reset the accessory type for dequeued cells.

这应该为你解决;

  cell.accessoryType = UITableViewCellAccessoryNone;

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

方便提示:您可能会发现使用switch(indexPath.section)而不是很多if ... elseif ... else构造。虽然你现在只有两个部分,我发现使用开关使得更容易放大节的数量,也使得它在心理上更容易阅读的代码,因为你经常有if .. else结构用于其他的东西 - 具有不同的语法有助于更清楚测试是什么。

Handy tip : You may find it useful to use switch(indexPath.section) rather than lots of if ... elseif ... else constructs. Although you're OK now with just 2 sections, I find that using switch makes it easier to scale up the number of sections and also makes it mentally easier to read throught the code since you often have if .. else constructs used for other things - having the different syntax helps make it clearer what the tests are for.

这篇关于一次只允许一个UITableViewCellAccessoryCheckmark的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆