0

3 つのセクションを含む UITableView があります。

  • failAtLaunchセクション 1 には、アクセサリ ビューにUISwitch を含む 1 つのセルがあります。
  • セクション 2 には、failSounds 配列からのアイテムが含まれ、選択されたアイテム (この場合はサウンド) の単一のチェックマークがあります。
  • セクション 3 にはボタンが含まれています。

セルの構成方法を以下に示します。

問題は、テーブルが奇妙な動作をすることです。テーブルビューを上下にスクロールすると、セルが表示されたり表示されなくなったりすると、セクション 2 の一部のセルがアクセサリビューで UISwitchfailAtLaunchを取得します。

これがなぜなのかを理解するのを手伝ってくれる人はいますか?

前もって感謝します!

  - (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 に答える