-1

私は自分のUISwitch内部に問題がありUITableViewCellます。1 つのスイッチの値を変更してから上下にスクロールすると、すべてのスイッチがめちゃくちゃになります。私は配列を使用して各スイッチの状態を保存していますが、再利用可能であるため、毎回混乱しています。

cellForRowAtIndexPathメソッドは次のとおりです。

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];


    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:@"Cell"];

    }

    UISwitch *switchController = [[UISwitch alloc] initWithFrame:CGRectZero];
    CGRect switchFrame = switchController.frame;
   [switchController setOn:YES animated:NO];
    //set its x and y value, this you will have to determine how to space it on the left side
    switchFrame.origin.x = 50.0f;
    switchFrame.origin.y = 10.0f;
    switchController.frame = switchFrame;


    [switchController addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
    [cell addSubview:switchController ];

    UILabel *label ;

    label=(UILabel *)[cell viewWithTag:1];
    NSString *value = [[mainArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    label.text = value;
    label.textAlignment = NSTextAlignmentRight;
    label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;


    //This for persist switch state when scroll up or down
    if ([[[self.SwitchArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row ]isEqualToString:@"ON"])
    {
       switchController.on=YES;


    }
    else 
    {
       switchController.on=NO;


    }

       return cell;   
}

SwitchChanged イベントは次のとおりです。

-(void)switchChanged:(UISwitch *)sender
{
    UITableViewCell *cell = (UITableViewCell *)[[sender superview] superview];
    NSIndexPath *index=[mainTableView indexPathForCell:cell];

    if (sender.on)
    {

        [[self.SwitchArray objectAtIndex:index.section] replaceObjectAtIndex:index.row withObject:@"ON"];
        NSString *word= [[self.mainArray objectAtIndex:index.section ] objectAtIndex:index.row];

    }
    else
    {
        //call the first array by section
        [[self.SwitchArray objectAtIndex:index.section] replaceObjectAtIndex:index.row withObject:@"OFF"];
         NSString *word= [[self.mainArray objectAtIndex:index.section ] objectAtIndex:index.row];

   }

    [padFactoids setObject:[NSKeyedArchiver archivedDataWithRootObject:SwitchArray] forKey:@"savedArray"];
    [padFactoids synchronize];

}

あなたの助けに感謝します。

4

3 に答える 3

1

ヘッダー ファイルで NSMutableArray を宣言し、名前を switchStates にしましょ

viewDidLoad で、メモリを割り当て、スイッチの数に応じて文字列「OFF」でオブジェクトを追加します。

switchStates = [[NSMutableArray alloc] init];
for (int i; i <= switchesArray.count; i++) {
    [switchStates addObject:@"OFF"];
}

スイッチがトリガーされたときに実行されるメソッドで:

NSString *theSwitchPosition = [NSString stringWithFormat:@"%@", switchControl.on ? @"ON" : @"OFF"];
[switchStates replaceObjectAtIndex:aPath.row withObject:theSwitchPosition];

その後、スイッチを作成するメソッドで:

if ([[switchStates objectAtIndex:indexPath.row] isEqualToString:@"ON"]) {
    mySwitch.on = YES;
} else {
    mySwitch.on = NO;
}

これは私のために働いた、幸運..

于 2014-08-12T06:22:15.757 に答える
0

これがあなたの問題の原因かどうかはわかりませんが、他の関連する問題が発生することは確かです。

セルが画面外に移動され、次のセルが表示されるたびに、画面外に移動したばかりのセルが再利用されます。ただし、毎回新しいスイッチ オブジェクトをセルに追加します。cell==nilブロック内にのみ作成する方がはるかに優れています。ラベル オブジェクトの場合と同様に、タグを付けて、再利用時にオブジェクトを取得するためにタグを使用します。

于 2013-04-26T14:43:24.877 に答える
0

tableView がセルを要求するたびに、新しいスイッチを作成しています。スイッチはセルごとに 1 回だけ作成します。

UISwitch *switchController;

 if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                  reuseIdentifier:@"Cell"];

    switchController = [[UISwitch alloc] initWithFrame:CGRectZero];
    CGRect switchFrame = switchController.frame;
    [switchController setOn:YES animated:NO];
    //set its x and y value, this you will have to determine how to space it on the left side
    switchFrame.origin.x = 50.0f;
    switchFrame.origin.y = 10.0f;
    switchController.frame = switchFrame;

    [switchController addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
    [cell.contentView addSubview:switchController ];

    switchController.tag = 123; //Arbitrary number...can be anything 
}
else {
    switchController = (UISwitch *)[cell.contentView viewWithTag:123];
}

//Now set the switch state according to your data model array

また、通常は、セル自体ではなく、セルの contentView にサブビューを追加することをお勧めします。

于 2013-04-26T14:36:57.040 に答える