分段的UITableView在accesoryview中提供了意外的行为

|| 我有一个包含三个部分的UITableView。 第1节有一个单元格,其accesoryview中包含UISwitch
failAtLaunch
第2部分包含failSounds数组中的项目,并为所选项目(在此情况下为声音)具有单个复选标记 第3节包含一个按钮。 您可以在下面看到我如何配置单元。 问题在于该表给出了奇怪的行为。每当我在表格视图中上下移动,单元格进出视图时,第2节中的某些单元格都会在其accesoryview中获得UISwitch
failAtLaunch
。 谁能帮我理解为什么会这样吗? 提前致谢!
  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
     //....blablabla...

        // Configure the cell...

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


        if (indexPath.section == 0){


            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\";

            return cell;

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

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

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

        return cell;

        } else {

            cell = [tableView dequeueReusableCellWithIdentifier:@\"buttonCell\"];
            if (cell == nil) {
                cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@\"buttonCell\"] 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; 

        }



    }
    
已邀请:
问题出在dequeueReusableCellWithIdentifier:对每种类型的单元格使用不同的静态字符串。     
这是您的等等之类的部分。我认为您可能对所有3种类型的行都使用相同的重用标识符,而不在重用之前将其清除。  因此,当实例化一个新的tableViewCell时,您需要打开section并为每一个赋予唯一的标识符。  我通常要做的是使用“ \ CLEAR_ME_OUT \”之类的标签定义添加到单元格的每个视图,该标签被定义为任意大数。然后,当我重复使用一个单元格时,我会遍历子视图,并使用所有带有该标记的单元格removeFromSuperview。这样可以很好地重复使用。     

要回复问题请先登录注册