分段 UITableView 在 accesoryview 中给出意外行为

发布于 2024-11-01 03:14:54 字数 2770 浏览 4 评论 0原文

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

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

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

问题是该表给出了奇怪的行为。每当我上下滚动表格视图,并且单元格移入和移出视图时,第 2 部分中的某些单元格会在其附件视图中获得 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; 

        }



    }

I've got a UITableView containing three sections.

  • Section 1 has one cell containing a UISwitch failAtLaunch in its accesoryview
  • Section 2 contains the items from the failSounds array and has a singular checkmark for the selected item (sound in this case)
  • Section 3 contains a button.

You can see below how I configure the cells..

The problem is that the table gives strange behaviour. Whenever I scroll up and down the tableview, with cells moving in and out of view, some cells in section 2 get the UISwitch failAtLaunchin their accesoryview.

Can anyone help me understand why this is?

Thanks in advance!

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

        }



    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆悲凉 2024-11-08 03:14:54

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

The problem is from the dequeueReusableCellWithIdentifier: use different static strings for each type of cell.

眼角的笑意。 2024-11-08 03:14:54

这是你的巴拉巴拉巴拉部分。我认为您可能对所有 3 种类型的行使用相同的重用标识符,并且在重用之前没有清除它们。
因此,当您实例化一个新的 tableViewCell 时,您需要打开部分并为每个部分指定一个唯一的标识符。
我通常做的是使用“CLEAR_ME_OUT”之类的标签来标记添加到单元格的每个视图,该标签被定义为一些大的任意数字。然后,当我重新使用单元格时,我会循环遍历子视图并从Superview 中删除带有该标签的所有单元格。这样就可以干净地重复使用。

It's your blah blah blah section. I think you are probably using the same reuse-identifier for all 3 types of rows, and not clearing them out prior to re-use.
So when you instantiate a new tableViewCell, you need to switch on section and give each one a unique identifier.
What I usually do is tag every view that I add to a cell with a tag like "CLEAR_ME_OUT" that is defined as some large arbitrary number. Then when I re-use a cell, I loop through the sub-views and removeFromSuperview all cells with that tag. That way it's nice a clean for re-use.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文