Tai Squared
Tai Squared

Reputation: 12323

iPhone - How to determine in which cell a button was pressed in a custom UITableViewCell

I currently have a UITableView that is populated with a custom UITableViewCell that is in a separate nib. In the cell, there are two buttons that are wired to actions in the custom cell class. When I click one of the buttons, I can call the proper method, but I need to know which row the button was pressed in. The tag property for each button is coming as 0. I don't need to know when the entire cell is selected, just when a particular button is pressed, so I need to know which row it is so I can update the proper object.

Upvotes: 20

Views: 9836

Answers (10)

clearlight
clearlight

Reputation: 12625

Here's a Swift example...

The UIControl class is the superclass of various iOS widgets, including UIButton, because UIControl provides the target/action mechanism that sends out the event notifications. Therefore a generic way to handle this is as follows:

func actionHandler(control: UIControl)
   var indexPath = tableView.indexPathForCell(control.superview!.superview! as UITableViewCell)!
   var row = indexPath.row
}

Here's an example of setting up a button control to deliver the action. Alternatively, create an @IBAction and create the action visually with Interface Builder.

button.addTarget(self, action: "actionHandler:", forControlEvents: .TouchUpInside)

You can downcast UIControl parameter to UIButton, UIStepper, etc... as necessary. For example:

var button = control as UIButton

The superview of control is the UITableViewCell's contentView, whose subviews are the UIViews displayed in the cell (UIControl is a subclass of UIView). The superview of the content cell is the UITableViewCell itself. That's why this is a reliable mechanism and the superviews can be traversed with impunity.

Upvotes: 3

Fareed Alnamrouti
Fareed Alnamrouti

Reputation: 32182

this solution also works in IBAction connected using storyboard cell prototype

- (IBAction)viewMapPostsMarker:(UIButton*)sender{
    // button > cellContentView > cellScrollView > cell
    UITableViewCell *cell = (UITableViewCell *) sender.superview.superview.superview; 
    NSIndexPath *index = [self.mapPostsView indexPathForCell:cell];
    NSLog(@" cell at index %d",index.row);
}

Upvotes: 0

Rinju  Jain
Rinju Jain

Reputation: 1694

    [btnFavroite setAccessibilityValue:[NSString stringWithFormat:@"%d",indexPath.row]];
    [btnFavroite setAccessibilityLabel:btnFavroite.titleLabel.text];
    [btnFavroite addTarget:self action:@selector(btnFavClick:) forControlEvents:UIControlEventTouchUpInside];

-(void)btnFavClick:(id)sender{
    UIButton *btn=(UIButton *)sender;
    int index=[btn.accessibilityValue integerValue]]
}

Upvotes: 0

Edward Huynh
Edward Huynh

Reputation: 2917

For a implementation that is not dependent on tags or the view hierarchy do the following

- (void)btnPressed:(id)sender event:(id)event
{
  UITouch *touch = [[event allTouches] anyObject];
  CGPoint touchPoint = [touch locationInView:self.tableView];
  NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:touchPoint];
}

Upvotes: 5

Tinku George
Tinku George

Reputation: 615

There are multiple methods to fix the problem.

  1. You can use the "tag" property Give the value indexPath.row as the tag value for the button.
    btn.tag = indexPath.row;

Then in the button function, you can easily access the tag value and it will be the index for the clicked button.

-(void)btnClicked:(id)sender
{
    int index = [sender tag];
}
  1. You can use the layer property Add the indexPath as the value in the layer dictionary.

    [[btn layer] setValue:indexPath forKey:@"indexPath"];

This indexPath is accessible from the button action function.

-(void)btnClicked:(id)sender
{
    NSIndexPath *indexPath = [[sender layer] valueForKey:@"indexPath"];
    int index = indexPath.row;
}

With this method you can pass multiple values to the button function just by adding new objects in the dictionary with different keys.

Upvotes: 1

William Jockusch
William Jockusch

Reputation: 27335

Even easier:

-(IBAction) buttonPressed {
    NSIndexPath *myIndexPath = [(UITableView *)self.superview indexPathForCell: self];
    // do whatever you need to do with the information
}

Upvotes: 3

JOM
JOM

Reputation: 8207

Much easier solution is to define your button callback with (id)sender and use that to dig out the table row index. Here's some sample code:

- (IBAction)buttonWasPressed:(id)sender
{
    NSIndexPath *indexPath =
        [self.myTableView
         indexPathForCell:(UITableViewCell *)[[sender superview] superview]];
    NSUInteger row = indexPath.row;

    // Do something with row index
}

Most likely you used that same row index to create/fill the cell, so it should be trivial to identify what your button should now do. No need to play with tags and try to keep them in order!

Clarification: if you currently use -(IBAction)buttonWasPressed; just redefine it as -(IBAction)buttonWasPressed:(id)sender; The additional callback argument is there, no need to do anything extra to get it. Also remember to reconnect your button to new callback in Interface Builder!

Upvotes: 21

user201839
user201839

Reputation:

I have the same scenario. To achieve this, I derived a custom cell. I added two properties, section and row. I also added an owner, which would be my derived TableViewController class. When the cells are being asked for, I set the section/row based on the indexPath, along with the owner.

cell.section = indexPath.section cell.row = indexPath.row cell.owner = self

The next thing that I did was when I created the buttons, I associate the button events with the cell rather than with the tableViewController. The event handler can read the section and row entry and send the appropriate message (or event) to the TableViewController. This greatly simplifies house keeping and maintenance by leveraging existing methods and housekeeping and keeping the cell as self contained as possible. Since the system keeps track of cells already, why do it twice!

Upvotes: 4

Marco Mustapic
Marco Mustapic

Reputation: 3859

You can access the buttons superview to get the UITableViewCell that contains your button, but if you just need the row number, you can use the tag property like the previous post deacribes.

Upvotes: 1

pix0r
pix0r

Reputation: 31290

You could use the tag property on the button to specify which row the button was created in, if you're not using tags for anything else.

Upvotes: 10

Related Questions