2

I have a UIViewController with a table view. Table row contains buttons which trigger an action.

//This is my button selector inside tableview delegate cellForRowAtIndexPath

[myBtn addTarget:self action:@selector(onButtonPress:event:)forControlEvents:UIControlEventTouchUpInside];

//This is button action



 -(void)onButtonPress:(UIButton*)sender {
       CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.myTableView];
       NSIndexPath *indexPath = [self.myTableView indexPathForRowAtPoint:buttonPosition];

       DestinationViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"DestinationViewController"];
       controller.args=myArgs;
       [self.navigationController pushViewController:controller animated:YES];


 }

//this is my back button action

- (IBAction)onBackPressed:(id)sender {
     [self.navigationController popViewControllerAnimated:YES];
}

At first time all is working fine but after i navigated to other ViewController and return back to tableview controller by pressing back button and again click on table row button the NSIndexPath is nil

i have tried this indexPathForRowAtPoint returns nil only for first cell in a uitableview

Community
  • 1
  • 1
Askarc Ali
  • 318
  • 4
  • 21

5 Answers5

0

If I understand you correct, you have Buttons within TableViewCells and you don't get the IndexPath when you tap one of the buttons.

Of course is there no IndexPath since you never tapped a cell. Basically you ignore the complete tableview mechanism.

When creating the tableviewcell, tag the buttons with the index. Not the best approach, but will do.

Helge Becker
  • 3,219
  • 1
  • 20
  • 33
0

Set tag of button equal to indexPath.row and find the NSIndexPath from tag.

myBtn.tag=indexPath.row;
[myBtn addTarget:self action:@selector(onButtonPress:)forControlEvents:UIControlEventTouchUpInside];

-(void)onButtonPress:(UIButton*)sender {

UIButton *btnSender =(UIButton *)sender;

 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:btnSender.tag inSection:0];

}
darshan
  • 1,115
  • 1
  • 14
  • 29
0

I won't use the method indexPathForRowAtPoint: to get the indexPath. You can archive it with a different way. First add an UIView category:

Header file:

#import <UIKit/UIKit.h>

@interface UIView (TableCell)

-(UITableViewCell*)getTableCell;

@end

and implementation

@implementation UIView (TableCell)

-(UITableViewCell *)getTableCell
{
   while (self.superview) {
      if ([self.superview isKindOfClass:[UITableViewCell class]]) {
         return (UITableViewCell*)self.superview;
      }else {
         return [self.superview getTableCell];
      }
   }
   return nil;
}

Now, in your onButtonPress: you can do like this:

UITableViewCell *cell = [sender getTableCell];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]
sahara108
  • 2,829
  • 1
  • 22
  • 41
0

My suggest you can write [tableView reload] under 'viewWillAppear'; Or use other way like block to implement

jerry
  • 1
  • 2
0

Try sending a delegate to the nextviewcontroller like

   DestinationViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"DestinationViewController"];
   controller.args=myArgs;
   controller.delegate = self;
   [self.navigationController pushViewController:controller animated:YES];

and when traversing back from that viewController to the present table viewcontroller pass that delegate.

This may help you...

RStack
  • 230
  • 4
  • 16
  • thank you for your suggestion.how can i travers back ? can you give me sample?. because all my data in that should preserve. – Askarc Ali Nov 12 '15 at 10:55
  • Can you show me the code by which you are traversing to the present viewcontroller and the code by which you are showing the next viewcontroller – RStack Nov 13 '15 at 05:02
  • `controller.delegate = self;` add this line to your code – RStack Nov 16 '15 at 05:23