表单元格中的UIswitch重用 [英] UIswitch in a table cell reused

查看:127
本文介绍了表单元格中的UIswitch重用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a problem with uiswitch in my UITableViewCell that whenever i change a switch value in a specific cell that belongs to a specific section. All other sections cells that have same inexPath.row change . Please help ?

这是我的代码 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:

And here is 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];

}


推荐答案

你有两个主要问题:


  1. 此代码错误:

  1. This code is wrong:

  [cell addSubview:switchController ];

切勿向细胞添加子视图;只将它添加到单元格的 contentView

Never add a subview to the cell; add it only to the cell's contentView.

您每次都要通过<$ c添加切换$ C>的cellForRowAtIndexPath:。但这些细胞正在重复使用。所以有些单元已经拥有开关。因此,您将向一些单元格添加许多开关。

You are adding the switch every time through cellForRowAtIndexPath:. But these cells are being reused. So some cells already have the switch. Thus you are adding many switches to some cells.

这篇关于表单元格中的UIswitch重用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆