-1

我的UISwitch内部有问题UITableViewCell。当我更改一个开关的值时,向上或向下滚动所有开关都搞砸了。由于可重用性,我使用一个数组来存储每个开关的状态,但每次它们仍然搞砸了。

这是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

每次 tableView 要求一个单元格时,您都会创建一个新开关。您只想为每个单元格创建一次开关:

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 回答
0

我不确定这是否会导致您的问题,但肯定会导致其他相关问题。

每次将一个单元格移出屏幕并出现下一个单元格时,刚刚移出屏幕的单元格将被重新使用。但是您每次都向单元格添加一个新的开关对象。你最好只在cell==nil块内创建那些。给它一个标签,并使用标签在重用时获取对象,就像使用标签对象一样。

于 2013-04-26T14:43:24.877 回答