0

I have a tableview of which each cell has a custom button. I have this method which tells me the index path where the button was located. The method below works fantastic in iOS 6, but I am not sure how to get the same result in iOS 5 since indexPathForItem is only available in iOS 6.0 and later.

- (IBAction) checkUncheck:(id)sender
{
    UIButton *sendingButton = (UIButton *) sender;
    UITableViewCell *cell =  (UITableViewCell *)[[sendingButton superview] superview];
    PDFFile *newPDF = [_pdfDocument.pdfFileList objectAtIndex: cell.tag];
    [newPDF setCheck: [NSNumber numberWithBool: ![newPDF.check boolValue]]];

    NSIndexPath *path = [NSIndexPath indexPathForItem: cell.tag inSection: 0];

    [_table reloadRowsAtIndexPaths:[NSArray arrayWithObjects: path, nil] withRowAnimation: UITableViewRowAnimationAutomatic];
}
esqew
  • 42,425
  • 27
  • 92
  • 132
Joe Fratianni
  • 790
  • 8
  • 24
  • I think the actual answer is here: http://stackoverflow.com/a/16270198/365188 – Ozair Kafray Sep 17 '13 at 10:44
  • possible duplicate of [Getting row of UITableView cell on button press](http://stackoverflow.com/questions/7504421/getting-row-of-uitableview-cell-on-button-press) – Ozair Kafray Sep 17 '13 at 10:44

2 Answers2

1

There are 3 methods to find NSIndexPath in TableView

- (NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point;                         // returns nil if point is outside table
- (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell;                      // returns nil if cell is not visible
- (NSArray *)indexPathsForRowsInRect:(CGRect)rect;                              // returns nil if rect not valid 

You use indexPathForItem is not good method, try

NSIndexPath *path = [self.tableView indexPathForCell:cell] ;
LE SANG
  • 10,955
  • 7
  • 59
  • 78
  • Replacing this: NSIndexPath *path = [NSIndexPath indexPathForItem: cell.tag inSection: 0]; With this: NSIndexPath *path = [_table indexPathForCell: cell]; worked for me! – Joe Fratianni Jun 17 '13 at 01:34
0

Use a custom UITableViewCell subclass. When you configure your (hopefully reused) cell in -tableView:cellForRowAtIndexPath:, pass the index path (row and section) to the newly configured cell (i.e., each cell 'knows' its current index path).

Next, make the custom button notify the parent cell when tapped through the usual target-action mechanism. When the button is tapped, have the cell broadcast a notification with itself as the object property. Your table view controller can listen to this notification, extract the cell from it and the index path from the cell.

e.g., in the custom table cell implementation (MyTableViewCell.m):

- (void) didTapButton:(id) sender

{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"UserTappedCellButton" object:self];
}

in the view controller:

- (id) init 
{
    if(self = [super init]){
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(didTapCellButton:)
                                                     name:@"UserTappedCellButton"
                                                   object:nil];
    }
    return self;
}


- (void) didTapCellButton:(NSNotifcation*) notification
{
   MyCustomTableViewCell* cell = (MyCustomTableViewCell*)[notification object];

   NSIndexPath* path = [cell path]; // <-- You must define this property 

   // Use index path...
}

Perhaps there's a simpler/smarter way, but this one is pretty straightforward and comes to mind first...

Nicolas Miari
  • 16,006
  • 8
  • 81
  • 189