0

UITableViewCell每当我更改属于特定部分的特定单元格中的开关值时,我的 uiswitch 都有问题。具有相同inexPath.row变化的所有其他部分单元格。请帮忙 ?

这是我的 cellForRowAtIndexPath方法代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    //UISwitch *switchview = nil ;

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



    }


    UISwitch *switchController = [[UISwitch alloc] initWithFrame:CGRectZero];
    CGRect switchFrame = switchController.frame;
    NSString *str = [[self.SwitchArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    NSLog(@"string in cell%@", str);


    //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 ];
    [addSubview:switchController];


    if ([[[self.SwitchArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row ]isEqualToString:@"ON"])
 {
        switchController.on=YES;
 }
    else
    {
        switchController.on=NO;
    }


    return cell;

}

这是 SwitchChangedEvent:

-(void)switchChanged:(UISwitch *)sender
{
    UITableViewCell *cell = (UITableViewCell *)[sender 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

3

你有两个主要问题:

  1. 这段代码是错误的:

      [cell addSubview:switchController ];
    

    永远不要向单元格添加子视图;仅将其添加到单元格的contentView.

  2. 您每次都在添加开关cellForRowAtIndexPath:。但是这些细胞正在被重复使用。所以有些细胞已经有了开关。因此,您正在向某些单元格添加许多开关。

于 2013-04-25T20:01:36.063 回答
0

正如其他人已经指出的那样,您正在为每个单元格添加多个开关。自由贸易区:

UISwitch *switchController = nil;
if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                  reuseIdentifier:CellIdentifier];
    switchController = [[UISwitch alloc] initWithFrame:CGRectZero];
    switchController.tag = 'swch';
    CGRect switchFrame = switchController.frame;
    //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];
}
else 
{
    switchController = (UISwitch*)[cell.contentView viewWithTag: 'swch'];
    NSAssert(switchController != nil, @"how?");
}

NSString *str = [[self.SwitchArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
NSLog(@"string in cell%@", str);

switchController.on = [str isEqualToString:@"ON"];

return cell;
于 2013-04-26T11:14:36.930 回答
0

如果你看,我想你会发现你正在构建多个交换机并在每个单元上。

随着细胞被重复使用,您添加了一个新开关,但不要删除旧开关。我认为这至少是您的问题的部分原因。


更新

我想我会与其他人有所不同。我认为@AlanoudAldrees 删除旧开关会更容易。

if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                  reuseIdentifier:CellIdentifier];
}
else
{
    for (UIView *view in cell.subviews)
        if ([view isKindOfClass:[UISwitch class]])
            [view removeFromSuperview];
}
于 2013-04-25T20:00:27.287 回答