Select UITableView's row when clicking on UISwitch

后端 未结 7 831
眼角桃花
眼角桃花 2020-12-06 07:45

I have a UITableView with UISwitchs on them.

\"TableView\"

When the switch is

7条回答
  •  無奈伤痛
    2020-12-06 08:31

    #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
    

提交回复
热议问题