简体   繁体   中英

UISwitch exclusive in UITableViewCell

I have a UITableView with one UISwitch for row. To do it:

UISwitch *switchController = [[UISwitch alloc] initWithFrame:CGRectZero];
if([valor isEqualToString:@"true"])  
    [switchController setOn:YES animated:YES];
else
    [switchController setOn:NO animated:YES];

switchController.tag = row;
[switchController addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
cell.accessoryView = switchController;
[switchController release];

Ok, i want exclusive row, when i touch one UISwitch i need put all others UISwitch in OFF. But i can´t do it...Someone can help me?

Thanks friends.

-(void) switchChanged:(id)sender{
    UISwitch *switchController = sender;
}

One simple approach could be like this.

You want that user can switch on only one row at a time. For this you will need to keep track of two things first the switches(we can access it via cell's accessory view) and second the switch which is on.(for this suppose we use tag property).

So we will need:
>One variable to keep track of which row's switch is on.
>Array which will hold the of the switches.

I have created a sample app with following code:(I am taking number of rows as constant to 10)

Code

SwitchTableController.h

    #import <UIKit/UIKit.h>
#define NUMBER_OF_ROWS 10
        @interface SwitchTableController : UITableViewController {

            int selectedSwitchRow;
            NSMutableArray *switchArray;
        }

        @end

SwitchTableController.m code

#import "SwitchTableController.h"

@implementation SwitchTableController

#pragma mark -
#pragma mark View lifecycle

- (void)dealloc {
    [switchArray release];
    [super dealloc];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    selectedSwitchRow = -1;
    if (switchArray == nil)
    {
        switchArray  = [[NSMutableArray alloc] initWithCapacity:10];
    }
    for (int i=0; i<NUMBER_OF_ROWS; i++)
    {
        UISwitch *switchController = [[UISwitch alloc] initWithFrame:CGRectZero];
        [switchController setOn:NO animated:YES];
        switchController.tag = i;
        [switchController addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
        [switchArray addObject:switchController];
        [switchController release];
    }
}

#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return NUMBER_OF_ROWS;
}

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

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    cell.accessoryView = [switchArray objectAtIndex:indexPath.row];
   return cell;
}

-(void) switchChanged:(UISwitch *)sender
{
    if (selectedSwitchRow >= 0 && selectedSwitchRow<[switchArray count] && selectedSwitchRow != sender.tag)
    {
        UISwitch *tempSwitch = [switchArray objectAtIndex:selectedSwitchRow];
        [tempSwitch setOn:NO animated:YES];
        [self.tableView reloadData];
    }
    selectedSwitchRow = sender.tag;
}

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