Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 分区UITableView在accesoryview中提供意外行为_Iphone_Cocoa Touch_Uitableview_Accessoryview - Fatal编程技术网

Iphone 分区UITableView在accesoryview中提供意外行为

Iphone 分区UITableView在accesoryview中提供意外行为,iphone,cocoa-touch,uitableview,accessoryview,Iphone,Cocoa Touch,Uitableview,Accessoryview,我有一个包含三个部分的UITableView 第1节的accesoryview中有一个单元格,其中包含UISwitch failAtLaunch 第2节包含failSounds数组中的项目,在本例中,所选项目声音有一个单数复选标记 第3节包含一个按钮。 您可以在下面看到我如何配置单元格 问题是这张桌子表现出奇怪的行为。每当我在tableview上下滚动时,当单元格在视图中进出时,第2节中的某些单元格会在其AccessoryView中显示UI开关故障 有人能帮我理解为什么会这样吗 提前谢谢 -

我有一个包含三个部分的UITableView

第1节的accesoryview中有一个单元格,其中包含UISwitch failAtLaunch 第2节包含failSounds数组中的项目,在本例中,所选项目声音有一个单数复选标记 第3节包含一个按钮。 您可以在下面看到我如何配置单元格

问题是这张桌子表现出奇怪的行为。每当我在tableview上下滚动时,当单元格在视图中进出时,第2节中的某些单元格会在其AccessoryView中显示UI开关故障

有人能帮我理解为什么会这样吗

提前谢谢

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

        }



    }

这是你的废话区。我认为您可能对所有3种类型的行使用了相同的重用标识符,并且在重用之前没有清除它们。 所以,当您实例化一个新的tableViewCell时,您需要打开部分并为每个部分提供一个唯一的标识符。
我通常会用一个像CLEAR\u ME\u OUT这样的标记来标记我添加到单元格中的每个视图,该标记被定义为一个大的任意数。然后,当我重复使用一个单元格时,我会在子视图中循环,并从SuperView中删除所有带有该标记的单元格。这样就可以很好地重复使用了。

问题来自于dequeueReusableCellWithIdentifier:对每种类型的单元格使用不同的静态字符串