0

I have a UITableView for which I have created a custom UITableViewCell. Each row in tableview has a button. I want to know the section number on click of a button, so that I would know that from which section button has been clicked. I have already tried few things found on stack but nothing is working.

UIButton *b = sender; 
NSIndexPath *path = [NSIndexPath indexPathForRow:b.tag inSection:0]; 
NSLog(@"Row %d - Section : %d", path.row, path.section);
rog
  • 5,351
  • 5
  • 33
  • 40
Alex Bichel
  • 85
  • 1
  • 2
  • 5

4 Answers4

5

Don't know what you've tried, but i might do something like this. Doing some pseudocode from memory, here.

- (void)buttonClicked:(id)sender {
    CGPoint buttonOrigin = [sender frame].origin;
    // this converts the coordinate system of the origin from the button's superview to the table view's coordinate system.
    CGPoint originInTableView = [self.tableView convertPoint:buttonOrigin fromView:[sender superview];

    // gets the row corresponding to the converted point
    NSIndexPath rowIndexPath = [self.tableView indexPathForRowAtPoint:originInTableView];

    NSInteger section = [rowIndexPath section];

}

If I'm thinking clearly, this gives you flexibility in case the button's not directly inside the UITableView cell. Say, if you've nested inside some intermediary view.

Sadly, there doesn't seem to be an iOS equivalent of NSTableView's rowForView:

Jablair
  • 5,036
  • 4
  • 28
  • 37
  • I would suggest this way instead of doing sender.superView.superView because we can have any level of nest os view but this way work for all. – CRDave May 18 '13 at 06:15
  • But I like to suggest to use this one line instead of two line CGPoint originInTableView = [sender convertPoint:CGPointMake(0, 0) toView:self.tableView];. we can use either both will work fine. – CRDave May 18 '13 at 06:19
  • Yeah, @CRDave, that works, too. I fell on the side of verbosity for this answer so it'd be clearer what was happening. – Jablair May 19 '13 at 17:33
3

Create a handler for button click and add it in tableView:cellForRowAtIndexPath: method

- (void)buttonPressed:(UIButton *)button{

    UITableViewCell *cell = button.superView.superView;

    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    //Now you have indexPath of the cell 
    //do your stuff here

}
Anupdas
  • 10,211
  • 2
  • 35
  • 60
  • better way to do this.. -(IBAction)buttonClicked:(id)sender{ id cellView = sender; while ([cellView isKindOfClass:[UITableViewCell class]] == NO) { cellView = [cellView superview]; } UITableViewCell *cell = (UITableViewCell*)cellView; //Get cell id view = [cell superview]; while ([view isKindOfClass:[UITableView class]] == NO) { view = [view superview]; } UITableView *tableView = (UITableView *)view; //Get tableview NSIndexPath *indexPath = [tableView indexPathForCell:cell]; } – tumbudu Oct 04 '13 at 07:09
  • @knocker Thanks for your comment. But I believe `indexPathForRowAtPoint:` is a much recommended one. – Anupdas Oct 05 '13 at 00:08
  • yes, you are correct. my point here is getting cell from button button.superview.superView would not work always. id cellView = sender; while ([cellView isKindOfClass:[UITableViewCell class]] == NO) { cellView = [cellView superview]; } UITableViewCell cell = (UITableViewCell)cellView; //Get cell id this would be the proper way.. – tumbudu Oct 18 '13 at 06:08
0

When you create your custom UITableViewCell in cellForRowAtIndexPath you should pass it its section as a parameter. It could look like:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
    MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];

  if (!cell)
  { 
  cell = [[[MyCustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier" section:indexPath.section] autorelease];
  }

    return cell;
 }

Now your cell knows its section and you can use it when performing click method in MyCustomCell class

Andrey Chernukha
  • 21,488
  • 17
  • 97
  • 161
0

Try this,

First assign section as a tag to button also add target on button in cellForRowAtIndexPath method.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ...
    [cell.btnSample setTag:indexPath.section];
    [cell.btnSample addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    ...
}

Get Section as tag from sender of IBAction you defined (buttonClicked here).

-(IBAction)buttonClicked:(id)sender
{
    NSLog(@"Section: %d",[sender tag]);
}
βhargavḯ
  • 9,786
  • 1
  • 37
  • 59