1

私は奇妙な問題を抱えています(少なくとも私の意見では)。テーブル ビューに UISwitch を追加すると、ユーザーがテーブル ビューをスクロールすると、スイッチがセルを自動的に変更します。以下は、テーブルビューの UISwitch を作成する方法に関するコードです。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        //NSLog(@"This is what we are looking for %@    %@", checkUsername, checkPassword);
        // Configure the cell...
    NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section];
    NSArray *array =[dictionary objectForKey:@"Settings"];
    NSString * cellValue = [array objectAtIndex:indexPath.row];

    static NSString *CellIdentifier = @"Cell";
    NSUInteger section = [indexPath section];
    NSUInteger row = [indexPath row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell.accessoryView = nil;


    if (!cell) {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
        cell.textLabel.adjustsFontSizeToFitWidth = YES;
            //switcher.on = [[NSUserDefaults standardUserDefaults]boolForKey:@"Settings"];

        if (section == 1)
        {

            UISwitch *switchView = [[UISwitch alloc] init];
            cell.accessoryView = switchView;
            [switchView setOn:YES animated:NO];
            [switchView setTag:[indexPath row]];
            [switchView addTarget:self action:@selector(switchWasChangedFromEnabled:) forControlEvents:UIControlEventValueChanged];
                //[self.tableView reloadData];
            [switchView release];
        }

    }
    return cell;
}

スイッチが指定されたセルから移動し続ける理由と、考えられる解決策を教えてください。

ジャスティン・ギャラガーはこれを提案しました:

セルをキャッシュして再利用しているために発生しています。スイッチ付きのセルが別の場所で再利用されることがあります。スイッチでセルに別の識別子を設定してみてください。

しかし、セルごとに異なる識別子を設定する方法を教えてもらえますか?

乾杯、

分かりました

4

2 に答える 2

2

セルをキャッシュして再利用しているために発生しています。スイッチ付きのセルが別の場所で再利用されることがあります。スイッチでセルに別の識別子を設定してみてください。

于 2010-09-05T14:25:36.560 に答える
0

ここ[元のソース] に示されている次のコードを使用して、問題を解決しました。以下にコード スニペットを貼り付けました。また、このアイデアはジャスティン・ギャラガーから私に与えられました。ありがとうございました。

- (void)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (<indexPath is for cell type "A">) {
    static NSString *SomeIdentifierA = @"SomeIdentifierA";

        // This could be some custom table cell class if appropriate    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SomeIdentifierA];
    if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:SomeIdentifierA] autorelease];
            // Any other attributes of the cell that will never change for type "A" cells.
    }

        if (someCondition) {
            cell.textColor = <someColor>;
        } else {
            cell.textColor = <anotherColor>;
        }
        cell.text = <some text>;    
    return cell;
    } else if (<indexPath is for cell type "B") {
    static NSString *SomeIdentifierB = @"SomeIdentifierB";

        // This could be some custom table cell class if appropriate    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SomeIdentifierB];
    if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:SomeIdentifierB] autorelease];
            // Any other attributes of the cell that will never change for type "B" cells.
    }

        cell.text = <some text>;    
    return cell;
    } else {
        return nil; // Oops - should never get here
于 2010-09-05T17:33:32.793 に答える