简体   繁体   中英

SwitchView for each UITableViewcell in UITableView

I have added a switchview as an accessory view to the UItableviewcell, i have 5 entries in the table ie., 5 cells and switches for them in that row.. I have given each switch a tag value which is equal to indexpath.row (so 1st switch is 0, 2nd is 1 ... and 5th switch is 4)


UISwitch *switchView = [[UISwitch alloc]initWithFrame:CGRectZero];
cell.accessoryView = switchView;
[switchView setOn:NO animated:NO];
[switchView setTag:indexPath.row];

I have given a target method for the switch, the target method gets called when the value of switch is changed..

[switchView addTarget:self action:@selector(switchTimer:)   forControlEvents:UIControlEventValueChanged];

In this target method based on the change in event of the Switch I'm trying to building an array with count equal to the number of switches.. Eg: If Switch 0 and 3 are ON and Switch 1,2,4 are OFF, the array would look something like this

A = {"ON","OFF","OFF","ON","OFF"}

The following is the method which I'm using to achieve this:

-(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
}

The problem is every time a switch gets on, say switch 2 (with tag 1) all the switches will get the tag value 1, even if i iterate over all the switches with the help of their tag values. I want to know the status of all switches even when only 1 switch's value gets changed.. but that's not happening, all the switches get tag value 1 and i get an array like this

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

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

Can someone please help me out finding what the issue is? Thanks in advance

EDIT : the complete 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;

}

If you want to know the status of all switches even when only 1 switch's value gets changed try this,

-(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
}

As you are using the for loop, values of the switches are getting change. so you don't need to use for loop here. instead you can just replace the object at the index of your current switch.

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

    [array_Timers replaceObjectAtIndex: index withObject: currentValue];
}

What you have to do is: primarily initialise the Array with the Values of OFF, before the Table gets loaded . For Ex :

-(Void) ViewDidLoad {
   array_Timers = [NSArray alloc]... // Add the Objects .
}

In the UITableView Delegate : (don't try to set Tag as 0 to any elements )

UISwitch *switchView = [[UISwitch alloc]initWithFrame:CGRectZero];
cell.accessoryView = switchView;
[switchView setOn:NO animated:NO];
[switchView setTag:indexPath.row+25]; //don't try to set Tag as 0 to any elements 
[switchView addTarget:self action:@selector(switchTimer:)   forControlEvents:UIControlEventValueChanged];

Then just replace with the values according to the events

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

NSLog (@" Values %@", array_Timers );
}

Try this...

-(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

Note - If you are reusing your switches and the order in array_Timers is not consistent with the order in your table view, you should sort your switches by tag first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM