I have a UITableView
with UISwitchs
on them.
When the switch is
#import
@interface ViewController : UIViewController
@property (retain, nonatomic) IBOutlet UITableView *tableview;
@end
@interface ViewController ()
{
NSMutableArray *arr;
UITableViewCell* aCell;
UISwitch *myswitch;
}
#import "ViewController.h"
@interface ViewController ()
{
NSMutableArray *arr;
UITableViewCell* aCell;
UISwitch *myswitch;
}
@end
@implementation ViewController
@synthesize tableview;
- (void)viewDidLoad
{
[super viewDidLoad];
arr = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
return [arr count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
aCell = [tableview dequeueReusableCellWithIdentifier:@"SwitchCell"];
if( aCell == nil )
{
aCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SwitchCell"];
aCell.textLabel.text = [arr objectAtIndex:indexPath.row];
myswitch = [[UISwitch alloc]initWithFrame:CGRectZero];
aCell.accessoryView = myswitch;
[myswitch setOn:YES animated:YES];
}
switch (indexPath.row)
{
case 0:
{
[myswitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
}
break;
case 1:
{
[myswitch addTarget:self action:@selector(switchChanged1:) forControlEvents:UIControlEventValueChanged];
}
break;
}
return aCell;
}
- (void) switchChanged:(id)sender {
UISwitch *aswitch = sender;
if (aswitch.on==YES) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"hello" message:@"okfine" delegate:self cancelButtonTitle:@"done" otherButtonTitles:nil, nil];
[alert show];
}
else
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"no1" message:@"notfine" delegate:self cancelButtonTitle:@"Returnback" otherButtonTitles:nil, nil];
[alert show];
}
}
- (void) switchChanged1:(id)sender {
UISwitch *aswitch1 = sender;
if (aswitch1.on==YES) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"second" message:@"okfine" delegate:self cancelButtonTitle:@"done" otherButtonTitles:nil, nil];
[alert show];
}
else
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"no2" message:@"notfine" delegate:self cancelButtonTitle:@"Returnback" otherButtonTitles:nil, nil];
[alert show];
}
}
- (void)dealloc {
[tableview release];
[super dealloc];
}
@end