0

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

  • failAtLaunch第 1 节的一个单元格在其附件视图中包含一个 UISwitch
  • 第 2 部分包含来自 failSounds 数组的项目,并且对所选项目(在本例中为声音)有一个单数复选标记
  • 第 3 节包含一个按钮。

您可以在下面看到我如何配置单元格..

问题是该表给出了奇怪的行为。每当我向上和向下滚动表格视图时,单元格会移入和移出视图,第 2 节中的某些单元格会failAtLaunch在其附件视图中获取 UISwitch。

谁能帮我理解这是为什么?

提前致谢!

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

        }



    }
4

2 回答 2

0

这是你的废话部分。我认为您可能对所有 3 种类型的行都使用相同的重用标识符,而不是在重用之前清除它们。因此,当您实例化一个新的 tableViewCell 时,您需要打开部分并给每个部分一个唯一的标识符。我通常做的是标记我添加到单元格的每个视图,并使用“CLEAR_ME_OUT”之类的标记,该标记被定义为一些大的任意数字。然后,当我重新使用一个单元格时,我会遍历子视图并 removeFromSuperview 所有带有该标签的单元格。这样就可以很好地清洁以供重复使用。

于 2011-04-14T13:20:11.260 回答
0

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

于 2011-04-14T13:22:45.693 回答