单击UISwitch时selectUITableView的行

我有一个UITableView与他们的UISwitchs

的TableView

当开关切换时,我想运行一个function。 该function只logging如果开关打开或closures,并且开关已被更改的行。 我的问题是,当我点击开关时,它不会logging正确的行,除非在点击开关之前点击了那行。

我想我的问题是,单击开关不select行。 我怎样才能使它select行或我可以添加到交换机的ID?

所以开关ID“1”是“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" ); } 

find保存开关的单元

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

find该单元格的索引path

 NSIndexPath * indexpath = [myTableView indexPathForCell:cell] 

在你的情况

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

您应该将IndexPath.row作为标签设置为cellForRowAtIndexPath方法中的每个Switch

  switchView.tag= indexPath.row; 

当开关值改变时,你会得到行号

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

}

您可以检索在tableview中更改的UISwitch的NSIndexPath。 这是任何控制相同的想法已经在这篇文章中回答: 检测UITableView中按下了哪个UIButton

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

这将工作于iOS7,以前我使用[发件人超级检视],但现在返回UITableViewCellScrollView内的UITableViewCellContentView。

我不得不做双倍mySwitch.superview.superview来获得适当的单元格。

这是一个例子

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

我认为问题是你使用dequeueReusableCellWithIdentifier,并且你所有的单元格都有相同的id。

更好的方法是确定发件人在哪个单元格中。

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

一旦你有这个方法。 那么这是一个用[self.inputTableView indexPathForSelectedRow]replace的问题

 UITableViewCell *cell = [self findCellForView:sender]; NSIndexPath *indexPath = [self.inputTableView indexPathForCell:cell]; 
 #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