2013-04-25 29 views
0

我在我的UITableViewCell中遇到了uiswitch问题,每当我在属于特定部分的特定单元格中更改开关值时。所有其他部分单元格具有相同的inexPath.row更改。请帮忙 ?重新使用表格单元格中的UI开关

这里是我的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]; 

} 
+0

您有问题吗? – matt 2013-04-25 19:44:15

+0

'switchChanged:'中的代码看起来像什么? – jszumski 2013-04-25 19:44:45

+0

@jszumski这是它: – 2013-04-25 19:47:49

回答

0

如果你看看,我想你会发现你正在建立多个交换机和各细胞。

随着单元重新使用,您添加一个新开关但不删除旧单元。我认为这至少是你问题原因的一部分。


UPDATE

我想我会以不同方式处理这一点比其他人。我认为@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]; 
} 
+0

你能解释更多吗?因为我也认为这是我的问题谢谢 – 2013-04-25 20:09:22

+0

@AlanoudAldrees我更新了我的答案。 – 2013-04-26 15:08:56

3

你有两个主要问题:

  1. 这段代码是错误的:

    [cell addSubview:switchController ]; 
    

    绝不添加一个子视图到细胞;将其仅添加到单元的contentView

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

+0

非常感谢您的快速回复,这意味着很多..但是,请您给我详细说明如何解决第二点问题?我将不胜感激这么多,你将会是一种生活的节约 – 2013-04-25 20:04:59

+0

你已经在测试这是否是“virgin”单元格,并带有if(cell == nil)'。这种情况也是您需要添加交换机的地方。这样,如果单元格是* not * nil,您肯定知道它有一个开关。 – matt 2013-04-25 20:13:29

+0

它可能会帮助您阅读我关于此主题的书籍章节:http://www.apeth.com/iOSBook/ch21.html#_table_view_cells – matt 2013-04-25 20:16:20

0

正如其他人已经指出的那样,您要为每个单元添加多个开关。 FTFY:

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; 
+0

我强烈劝阻'switchController.tag ='swch''。见:http://stackoverflow.com/questions/7755202/multi-character-constant-warnings – 2013-04-26 14:51:19

+0

@JefferyThomas很高兴知道。虽然,请帮助我理解为什么在这个特定情况下这是一个坏主意?我在这里看不到任何可移植性问题。 – Mar0ux 2013-04-26 14:55:08

+0

int x ='xxxx'在标准C中是未定义的行为(GCC和LLVM具有实现定义的行为)。我对它的真正问题是,它隐藏了标签的含义。标记是一个数字,不是4个字符的序列。 – 2013-04-26 15:23:00

相关问题