Search code examples
iphoneobjective-cios4

UIButton Event for Delete Row in UITableView


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];

Solution

  • 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;
    }