I want to place two buttons in each table view cell. When I click on button number one I want the app to show an alert message: \"You tapped button1 at indexpath:3,0\". My
Using indexPath as the tag value is OK when you have only one button in a UITableCell but if you want to track more you can use modulo operator:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath {
...
imageButton1.tag=indexPath.row*2;
[imageButton1 addTarget:self action:@selector(buttonPushed:)
[cell.contentView addSubview:imageButton];
...
imageButton2.tag=indexPath.row*2+1;
[imageButton2 addTarget:self action:@selector(buttonPushed:)
[cell.contentView addSubview:imageButton];
for the selector you can distinguish between the buttons and get the indexPath like this:
-(void) buttonPushed:(id)sender{
UIButton *button = (UIButton *)sender;
switch (button.tag%2) {
case 0: // imageButton1 is pressed
// to reach indexPath of the cell where the button exists you can use:
// ((button.tag-button.tag%2)/2)
break;
case 1: // imageButton2 is pressed
break;
}
The example is for 2 buttons but you can adjust it according to the number of buttons.