views:

107

answers:

2

I add button in UITableView cell. My code is

UIButton *btnView = [[UIButton buttonWithType:UIButtonTypeContactAdd] retain];
    btnView.frame = CGRectMake(280.0, 8.0, 25.0, 25.0);

    //[btnView setImage:[UIImage imageNamed:@"invite.png"] forState:UIControlStateNormal];
    [btnView addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:btnView];
    [btnView release];

My button action is

- (void)action:(id)sender
{

}

I want to know, how to get current table row index when click. I want to delete table row when click on button.

Edit:: I found a way for delete with animation

[listOfItems removeObjectAtIndex:indexPath.row];

[self.tableview deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
+2  A: 

I believe one approach is to store the row number in the button tag

btnView.tag = [indexPath row]

in the action

- (void)action:(id)sender
{
    UIButton * btn = (UIButton)sender;
    int row = btn.tag;
}
Aaron Saunders
I'm using int row=[sender tag];
saturngod
A: 

I usually use this method:

- (IBAction)buttonAction:(id)sender {
    UIButton *button = sender;
    UIView *contentView = [button superview];
    UITableViewCell *cell = (UITableViewCell *)[contentView superview];
    NSIndexPath *indexPath = [tableView indexPathForCell:cell];

    //Do something
}

Depending on the layout of your UITableViewCell it is possible that you have to traverse deeper into your TableViewCell Views.

fluchtpunkt