2012-02-28 5 views
0

私は行に1つのUISwitchを持つUITableViewを持っています。これを行うには:私はOFFに他のすべてUISwitchを置く必要がある1つUISwitchをタッチするとUITableはUITableViewCellで排他的です

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]を、私は、排他的な行をしたいです。 しかし、私はそれをすることはできません...誰かが私を助けることができますか?

ありがとうございます。

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

スイッチの情報は何ですか? – Ravin

+0

すべてのvalorをfalseに設定し、[table reloadData]; – NeverBe

答えて

1

簡単なアプローチの1つは、次のようなものです。

ユーザーは、一度に1つの行だけをオンにすることができます。このためにはまずスイッチ(セルのアクセサリービューを介してアクセスできる)とスイッチがオンのスイッチの2つを追跡する必要があります(タグプロパティを使用するとします)。

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

私は次のコードでサンプルアプリを作成しました:(私は10に定数として行数を取っています)

コード

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; 
} 
関連する問題