IOS 7 – Comment get l'indexPath du button placé dans UITableViewCell

J'ai créé par programme un UITableView et ai ajouté UISwitch à sa vue d'accessoire de cellule.

C'est mon code pour UISwitch dans la vue accessoire de cellule dans la méthode cellForRowAtIndexPath .

 UISwitch *accessorySwitch = [[UISwitch alloc]initWithFrame:CGRectZero]; [accessorySwitch setOn:NO animated:YES]; [accessorySwitch addTarget:self action:@selector(changeSwitch:) forControlEvents:UIControlEventValueChanged]; cell.accessoryView = accessorySwitch; 

C'est la méthode qui est appelée après que le button est cliqué.

 - (void)changeSwitch:(UISwitch *)sender{ UITableViewCell *cell = (UITableViewCell *)[sender superview]; NSIndexPath *indexPath = [self.filterTableView indexPathForCell:cell]; NSLog(@"%ld",(long)indexPath); ……………. My other code……. } 

Je suis en mesure d'imprimer la valeur du path d'index dans iOS 6 Mais dans iOS 7, il imprime zéro,

Est-ce que je manque quelque chose dans iOS 7 ou il y a une autre approche pour get l'indexPath dans iOS 7

Merci, Arun.

NSLog(@"%ld",(long)indexPath); est faux cela va imprimer l'adresse du pointeur de indexPath

essayez d'utiliser les codes suivants

 CGPoint center= sender.center; CGPoint rootViewPoint = [sender.superview convertPoint:center toView:self.filterTableView]; NSIndexPath *indexPath = [self.filterTableView indexPathForRowAtPoint:rootViewPoint]; NSLog(@"%@",indexPath); 

Si vous avez un button dans la cellule. Vous pouvez get une cellule en appelant superview. Et puis peut get Indexpath de cette façon.

  (void)obButtonTap:(UIButton *)sender { UITableViewCell *cell = (UITableViewCell *)sender.superview; NSIndexPath *indexPath = [tableView indexPathForCell:cell]; } 

Solution rapide: Une extension UITableView comme celle-ci peut être utile pour cela.

 extension UITableView { func indexPathForView(view: AnyObject) -> NSIndexPath? { let originInTableView = self.convertPoint(CGPointZero, fromView: (view as! UIView)) return self.indexPathForRowAtPoint(originInTableView) } } 

L'utilisation devient facile partout.

 let indexPath = tableView.indexPathForView(button) 

Vous pouvez assigner des balises à chaque commutateur dans la méthode cellForRowAtIndexPath comme

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UISwitch *accessorySwitch = [[UISwitch alloc]initWithFrame:CGRectZero]; [accessorySwitch setOn:NO animated:YES]; [accessorySwitch addTarget:self action:@selector(changeSwitch:) forControlEvents:UIControlEventValueChanged]; cell.accessoryView = accessorySwitch; accessorySwitch.tag = indexPath.row; } - (void)changeSwitch:(UISwitch *)sender{ NSIndexPath *indexPath = [NSIndexPath indexPathWithIndex:[sender tag]]; NSLog(@"%ld",(long)indexPath); } 

Vous êtes probablement brûlé, en supposant que la vue d'set du commutateur va être la tableViewCell . Peut-être dans iOS 7 ils ont changé la hiérarchie pour get une sorte d'effet visuel; peut-être y a-t-il une nouvelle vue insérée là-dedans.

Vous pourriez éventuellement sous- UISwitch et lui donner une propriété indexPath . Ensuite, dans votre cellForRowAtIndexPath , assignez-le ici pour avoir une reference.

Je pense qu'il est dangereux de se fier à la position du button pour savoir lequel a été cliqué.

Je préfère créer un dictionary en prenant comme key le button / switch lui-même, et en tant que valeur le path d'index:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ ... UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; NSValue * accessorySwitchKey = [NSValue valueWithNonretainedObject:[cell settingSwitch]]; [[self switchIndexDictionary]setObject:indexPath accessorySwitchKey]; ... } 

puis quand le button / button est déclenché, j'obtiens le path d'index facilement de mon dictionary:

 - (void)toggleSetting:(id)sender { UISwitch *selectedSwitch = (UISwitch *) sender; NSValue * accessorySwitchKey = [NSValue valueWithNonretainedObject:selectedSwitch]; NSIndexPath *indexPath = [[self switchIndexDictionary]objectForKey: accessorySwitchKey]; } 

SWIFT 4:

 extension UITableView { func indexPathForView(view: AnyObject) -> NSIndexPath? { let originInTableView = self.convert(CGPoint.zero, from: (view as! UIView)) return self.indexPathForRow(at: originInTableView)! as NSIndexPath } }