I have added UIButton
in UITableViewCells
. I have when the user clicks the button we have get the indexpath to use the values from NSMutableA
Add Your UIButton
Like this
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setFrame:CGRectMake(10.0, 2.0, 140.0, 40.0)];
[btn setTitle:@"ButtonTitle" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[btn setTag:indexPath.row];
[cell.contentView addSubview:btn];
And then get its tag number -
-(void)buttonClicked:(id)sender
{
NSLog(@"tag number is = %d",[sender tag]);
//In this case the tag number of button will be same as your cellIndex.
// You can make your cell from this.
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[sender tag] inSection:0];
UITableViewCell *cell = [tblView cellForRowAtIndexPath:indexPath];
}
Note: Above solution will work when your tableView has only 1 section. If your tableView has more than one section either you should know your section index or go for below methods.
Alternative:1
UIView *contentView = (UIView *)[sender superview];
UITableViewCell *cell = (UITableViewCell *)[contentView superview];
NSIndexPath *indexPath = [tblView indexPathForCell:cell];
Alternative:2
CGPoint touchPoint = [sender convertPoint:CGPointZero toView:tblView];
NSIndexPath *indexPath = [tblView indexPathForRowAtPoint:touchPoint];
UITableViewCell *cell = [tblView cellForRowAtIndexPath:indexPath];
Given that the button is a subview of the cell view, and that the table view knows the index path of the cell view, something like this would work:
- (UITableViewCell *)containingCellForView:(UIView *)view
{
if (!view.superview)
return nil;
if ([view.superview isKindOfClass:[UITableViewCell class]])
return (UITableViewCell *)view.superview;
return [self containingCellForView:view.superview];
}
- (IBAction)buttonClicked:(id)sender
{
UITableViewCell *containingCell = [self containingCellForView:sender];
if (containingCell) {
NSIndexPath *indexPath = [self.tableView indexPathForCell:containingCell];
NSLog(@"Section: %i Row: %i", indexPath.section, indexPath.row);
}
}
For a tableview with more than one section a simpler solution would be to assign the tag:
cell.button.tag = indexPath.section * 1000 + indexPath.row
In the event handler use:
let section = sender.tag / 1000
let row = sender.tag % 1000
Note that you need to multiply with a larger number than 1000 if any of your sections can contain 1000 or more rows.
for me works if add tag to button in the cellForRowAtIndexPath...
cell.button.tag = indexPath.row;
and in the cell class get the tag... For example:
NSLog(@"tag number is = %d",[sender tag]);
whit that you can know the cell.row. well this works for me, Sorry for my english.
If you only need the table row, then you can set the button's tag property to the row number. If you need the whole path (with section number) then you'll have to subclass UIButton and make a new property ("indexPath" would be a good name) that you would set when you add the button to the table row.