Select UITableView's row when clicking on UISwitch

匿名 (未验证) 提交于 2019-12-03 01:54:01

问题:

I have a UITableView with UISwitchs on them.

When the switch is toggled I want to run a function. The function just logs If the switch is on or off and the row that the switch has been changed on. The problem that im having is that when I click on the switch it does not log the correct row unless I have clicked on that row before clicking the switch.

I guess my problem is that clicking the switch does not select the row. How can I make it so that it either selects the row or can I add the ID to the switch?

So switch ID "1" is "ON".

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{     static NSString *CellIdentifier = @"POICell";     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];       //set the cell text to the catName     cell.textLabel.text = [self.catNames objectAtIndex:[indexPath row]];     //add switch      cell.selectionStyle = UITableViewCellSelectionStyleNone;     UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero];     cell.accessoryView = switchView;     [switchView setOn:YES animated:NO];      [switchView addTarget:self action:@selector(switchChanged: ) forControlEvents:UIControlEventValueChanged];     // Configure the cell...      return cell; }       - (void) switchChanged:(id)sender {             NSString *StrCatID =[[NSString alloc]init];             StrCatID = [self.catIDs objectAtIndex:[self.inputTableView indexPathForSelectedRow].row];             UISwitch* switchControl = sender;             NSLog( @"The switch for item %@ is %@",StrCatID, switchControl.on ? @"ON" : @"OFF" );         } 

回答1:

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" );         } 


回答2:

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.    

}



回答3:

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.



回答4:

I had to do double mySwitch.superview.superview to get the proper cell.

Here's an example

- (void)switchToggle:(UISwitch *)mySwitch {   UITableViewCell *cell = (UITableViewCell *)mySwitch.superview.superview;  NSIndexPath *indexpath = [self.tableView indexPathForCell:cell];  NSLog(@"toggle section %d rowID %d", indexpath.section, indexpath.row);  } 


回答5:

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



回答6:

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]; 


回答7:

#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 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!