Select UITableView's row when clicking on UISwitch

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

I have a UITableView with UISwitchs on them.

\"TableView\"

When the switch is

相关标签:
7条回答
  • 2020-12-06 08:28

    A better way to do this is determine which cell the sender is in.

    - (UITableViewCell *)findCellForView:(UIView *)view
    {
        for (; view != nil; view = view.superview)
            if ([view isKindOfClass:[UITableViewCell class]])
                return view;
        return nil;
    }
    

    Once you have this method. then it's a matter of replacing [self.inputTableView indexPathForSelectedRow] with

    UITableViewCell *cell = [self findCellForView:sender];
    NSIndexPath *indexPath = [self.inputTableView indexPathForCell:cell];
    
    0 讨论(0)
  • 2020-12-06 08:29

    You should set the IndexPath.row as a Tag to each Switch in cellForRowAtIndexPath Method

     switchView.tag= indexPath.row;
    

    And when switch value change .you'll get the Row number

    - (void) switchChanged:(UISwitch *)sender {
      int rowIndex =[sender tag];
      //rowIndex you may use it further as you wanted.   
    

    }

    0 讨论(0)
  • 2020-12-06 08:31
    #import <UIKit/UIKit.h>
    
    @interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
    @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
    
    0 讨论(0)
  • 2020-12-06 08:39

    To find the cell that holds the switch

    UISwitch *switchInCell = (UISwitch *)sender;
    UITableViewCell * cell = (UITableViewCell*) swithInCell.superview;
    

    To find the indexpath of that cell

    NSIndexPath * indexpath = [myTableView indexPathForCell:cell]
    

    In your case

     - (void) switchChanged:(id)sender {
    
             UISwitch *switchInCell = (UISwitch *)sender;
             UITableViewCell * cell = (UITableViewCell*) swithInCell.superview;
             NSIndexPath * indexpath = [myTableView indexPathForCell:cell]
             NSString *strCatID =[[NSString alloc]init];
             strCatID = [self.catIDs objectAtIndex:indexpath];
             NSLog( @"The switch for item %@ is %@",StrCatID, switchInCell.on ? @"ON" : @"OFF" );
            }
    
    0 讨论(0)
  • 2020-12-06 08:42

    You can retrieve the NSIndexPath for the UISwitch that was changed in the tableview. This is the same idea for any control as already answered in this post : Detecting which UIButton was pressed in a UITableView

    - (void) switchChanged:(id)sender 
    {
        CGPoint switchPositionPoint = [sender convertPoint:CGPointZero toView:[self tableView]];
        NSIndexPath *indexPath = [[self tableView] indexPathForRowAtPoint:switchPositionPoint];
    }
    

    This will work for iOS7, previously I used [sender superview] but that now returns a UITableViewCellContentView inside of a UITableViewCellScrollView.

    0 讨论(0)
  • 2020-12-06 08:45

    I think the problem is that you use dequeueReusableCellWithIdentifier, and all of your cell has the same id.

    0 讨论(0)
提交回复
热议问题