views:

1128

answers:

5

i have tableview with custom cell.the table is divided in many section and rows.i have a custom button on cell. now i want to get section number and row number when i click on that button.? any idea regarding this

+4  A: 

The following method is called when you select a cell in your tableView:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

to access the section number: int section = indexPath.section;

to access the row number (within the correct section): int row = indexPath.row;

zPesk
That isn't called when the user taps a button on a row; it's only called if they tap the row background itself.
Jim Dovey
+2  A: 

The UITableView can convert a CGPoint coordinate into an indexPath:

-(NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point
Remus Rusanu
+1  A: 

A few thoughts:

You can iterate through the button's superview hierarchy until you find a UITableViewCell, then call - (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell on the UITableView.

- (void)buttonClicked:(id)sender {
  UIView *button = sender;

  for (UIView *parent = [button superview]; parent != nil; parent = [parent superview]) {
    if ([parent isKindOfClass: [UITableViewCell class]]) {
      UITableViewCell *cell = (UITableViewCell *) parent;     
      NSIndexPath *path = [self.tableView indexPathForCell: cell];

      // now use the index path

      break; // for
    }
  }
}

You can alternately use the tag of the button to store an index referencing the row. This only holds a single integer, so it makes the most sense when you have a single section, or when you are managing your rows as a flat list.

You can alternately subclass UITableViewCell to encapsulate the button. Your UITableViewCell could respond to the button events and rebroadcast an event to its own delegate, passing self. The event delegate can then call - (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell on the UITableView to get the index path.

dmercredi
A: 

Add can instance variable of your UITableViewCell subclassTo store the index path of the cell:

NSIndexPath *myIndexPath;

When you create the cell in:

cellForIndexPath:

pass in the indexpath to the newly created/recycled cell.

Now when you press the button, just read the indexpath from the ivar of your cell.

Corey Floyd
+2  A: 

You'll need to implement a UIControl event-handling method on your view controller, and set that as the handler for all your buttons. i.e. inside your -tableView:cellForRowAtIndexPath: function you would do something like:

[theCell.button addTarget: self
                   action: @selector(buttonPressed:withEvent:)
         forControlEvents: UIControlEventTouchUpInside];

Then your event handler would look like this:

- (void) buttonPressed: (id) sender withEvent: (UIEvent *) event
{
    UITouch * touch = [[event touches] anyObject];
    CGPoint location = [touch locationInView: self.tableView];
    NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint: location];

    /* indexPath contains the index of the row containing the button */
    /* do whatever it is you need to do with the row data now */
}
Jim Dovey
slight correction ... [event touches] should be [event allTouches]
dizy
great! this is exactly what I am looking for... thanks..
mshsayem