Ios UITableView中每个UITableViewcell的开关视图

Ios UITableView中每个UITableViewcell的开关视图,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我在UItableviewcell中添加了一个switchview作为附件视图,表中有5个条目,即该行有5个单元格和开关。。我给了每个开关一个标签值,它等于indexpath.row(所以第一个开关是0,第二个是1…第五个开关是4) 我为开关提供了一个目标方法,当开关的值更改时,会调用目标方法 [switchView addTarget:self action:@selector(switchTimer:) forControlEvents:UIControlEventValueChang

我在UItableviewcell中添加了一个switchview作为附件视图,表中有5个条目,即该行有5个单元格和开关。。我给了每个开关一个标签值,它等于indexpath.row(所以第一个开关是0,第二个是1…第五个开关是4)


我为开关提供了一个目标方法,当开关的值更改时,会调用目标方法

[switchView addTarget:self action:@selector(switchTimer:)   forControlEvents:UIControlEventValueChanged];
在这个基于开关事件变化的目标方法中,我试图构建一个计数等于开关数量的数组。。 例如:如果开关0和3打开,开关1、2、4关闭,阵列将如下所示

A = {"ON","OFF","OFF","ON","OFF"}
A = {"ON","ON","ON","ON","ON"}//when switch 2 is ON

A = {"OFF","OFF","OFF","OFF","OFF"}//when switch 2 is OFF
以下是我用来实现这一目标的方法:

-(void)switchTimer:(UISwitch *)senderSwitch{
       NSLog(@"Sender Switch Tag is %d",senderSwitch.tag);

       for(int i=0;i<[array_Timers count];i++){//Start for loop

         senderSwitch.tag = i;

         if(senderSwitch.on){
               NSLog(@"%@ is ON",[array_Timers objectAtIndex:i]);
               //Here I add an object to the array with value "ON"

         }
         else{
               NSLog(@"%@ is OFF",[array_Timers objectAtIndex:i]);
              //Here I add an object to the array with value "OFF"
         }

     }//End for loop
}
有人能帮我找出问题所在吗? 提前谢谢

编辑:完整的cellForRowAtIndexPath

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

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell  *cell = [timersTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell.selectionStyle = UITableViewCellSelectionStyleGray;


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

    UISwitch *switchView = [[UISwitch alloc]initWithFrame:CGRectZero];
    cell.accessoryView = switchView;
    [switchView setOn:NO animated:NO];
    [switchView setTag:indexPath.row];
    [switchView addTarget:self action:@selector(switchTimer:) forControlEvents:UIControlEventValueChanged];
    [switchView release];


    UIView *v = [[[UIView alloc] init] autorelease];
    v.backgroundColor = [UIColor orangeColor];
    cell.selectedBackgroundView = v;

}
UserTimer *usertimer = [array_Timers objectAtIndex:indexPath.row];//irrelevant here
[cell.textLabel setText:usertimer.alias];


return cell;

}

您要做的是:在加载表之前,首先用OFF值初始化数组。例如:

-(Void) ViewDidLoad {
   array_Timers = [NSArray alloc]... // Add the Objects .
}
在UITableView委托中:(不要尝试将任何元素的标记设置为0)

然后根据事件用值替换

-(void)switchTimer:(UISwitch *)senderSwitch{
      [array_Timers replaceObjectAtIndex:0 withObject:[[NSNumber numberWithBool:senderSwitch - 25]];

NSLog (@" Values %@", array_Timers );
}
试试这个

-(void)switchTimer:(UISwitch *)senderSwitch{
   NSLog(@"Sender Switch Tag is %d",senderSwitch.tag);

   for (UISwitch *mySwitch in switchesArray) {

     if(mySwitch.on){
           NSLog(@"%@ is ON",mySwitch);
           //Here I add an object to the array with value "ON"

     }
     else{
           NSLog(@"%@ is OFF",mySwitch);
          //Here I add an object to the array with value "OFF"
     }

 }//End for loop

注意-如果重复使用交换机,并且数组计时器中的顺序与表视图中的顺序不一致,则应首先按标签对交换机进行排序。

如果您想知道所有交换机的状态,即使只有一个交换机的值发生了更改,也可以尝试此操作

-(void)switchTimer:(UISwitch *)senderSwitch{
       NSLog(@"Sender Switch Tag is %d",senderSwitch.tag);

       for(int i=0;i<[array_Timers count];i++){//Start for loop
         NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
         UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
         UISwitch *switch = (UISwitch *)[cell.contentView viewWithTag:indexPath.row];

         if(switch.on){
               NSLog(@"%@ is ON",[array_Timers objectAtIndex:i]);
               //Here I add an object to the array with value "ON"

         }
         else{
               NSLog(@"%@ is OFF",[array_Timers objectAtIndex:i]);
              //Here I add an object to the array with value "OFF"
         }

     }//End for loop
}
-(无效)开关计时器:(UISwitch*)发送器开关{
NSLog(@“发送方开关标签为%d”,发送方开关.Tag);

for(int i=0;i当您使用for循环时,开关的值正在发生变化。因此您不需要在此处使用for循环。相反,您可以在当前开关的索引处替换对象

-(void)switchTimer:(UISwitch *)senderSwitch {
    NSString *currentValue = @"";
    NSInteger index = senderSwitch.tag;
    if(senderSwitch.on){
        currentValue = @"ON";
    }
    else {
        currentValue = @"OFF";
    }

    [array_Timers replaceObjectAtIndex: index withObject: currentValue];
}

这是因为您在循环中检查了
if(senderSwitch.on)
。如果此按钮位于数组中的所有元素上,则变为
on
,反之亦然。如果我错了,请原谅我3个月前开始使用Objective C,我的疑问是,如果每行都有自己分配的开关,并且有一个开关指针(或一个引用)我可以根据分配的标记值浏览所有开关,并检查开关的状态..为什么不使用for循环来执行此操作?向我们展示您的cellForRowAtIndexPath。单元格重用可能会给您带来各种问题…@user2728987您可以通过分配的标记遍历所有开关。但不能通过
senderSwitch.tag=i;
,这只会更改
senderSwitch
的标记,而不会使用标记
i
@CrimsonChris引用开关。是的,您是对的,对于超过10行或更多的行,开关的打开/关闭行为非常奇怪。现在我知道为什么每次调用标记时都要将其设置为+25以避免误操作g带有未设置标记的开关和第0个开关?这不起作用我得到了一个数组超出范围异常:原因是数组索引和标记是相关的。数组索引0处的元素将是带有标记0的开关的行的名称------------------------------------------MasterComputer switch(关闭/打开)------------------------------------------------------------MasterComputer将是一个索引为0的元素,在我用来标记单元格的数组中。谢谢兄弟,这完全正确,我不必反复迭代。这是一个很好的解决方案
-(void)switchTimer:(UISwitch *)senderSwitch{
       NSLog(@"Sender Switch Tag is %d",senderSwitch.tag);

       for(int i=0;i<[array_Timers count];i++){//Start for loop
         NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
         UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
         UISwitch *switch = (UISwitch *)[cell.contentView viewWithTag:indexPath.row];

         if(switch.on){
               NSLog(@"%@ is ON",[array_Timers objectAtIndex:i]);
               //Here I add an object to the array with value "ON"

         }
         else{
               NSLog(@"%@ is OFF",[array_Timers objectAtIndex:i]);
              //Here I add an object to the array with value "OFF"
         }

     }//End for loop
}
-(void)switchTimer:(UISwitch *)senderSwitch {
    NSString *currentValue = @"";
    NSInteger index = senderSwitch.tag;
    if(senderSwitch.on){
        currentValue = @"ON";
    }
    else {
        currentValue = @"OFF";
    }

    [array_Timers replaceObjectAtIndex: index withObject: currentValue];
}