简体   繁体   English

UITableViewCell中专有的UISwitch

[英]UISwitch exclusive in UITableViewCell

I have a UITableView with one UISwitch for row. 我有一个带有一个UISwitch行的UITableView。 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. 好的,我要排他一行,当我触摸一个UISwitch时,我需要将所有其他UISwitch都置于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). 为此,您需要跟踪两件事:首先是开关(我们可以通过单元格的附件视图访问它),其次是打开的开关(为此我们使用tag属性)。

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) 我用以下代码创建了一个示例应用程序:(我将行数作为常量设为10)

Code

SwitchTableController.h SwitchTableController.h

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

            int selectedSwitchRow;
            NSMutableArray *switchArray;
        }

        @end

SwitchTableController.m code SwitchTableController.m代码

#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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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