简体   繁体   中英

Cannot delete row from custom tableview cell button

I have a tableview with a custom tableviewCell. In this tableview cell I have a button for deleting a row.

First of all I create this tableview by the following code.

   UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 70, 320, 150) style:UITableViewStylePlain];
    tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = [UIColor blackColor];
    tableView.separatorStyle = normal;
    [tableView reloadData];

I have an appointments array that I use as my datasource. In my cellForRowAtIndex I use the following code to attach my button to an action.

 cell.btnDelete.tag = indexPath.row;
    [cell.btnDelete addTarget:self action:@selector(deleteAppointment:) forControlEvents:UIControlEventTouchUpInside];

And then I have my action itself.

-(void)deleteAppointment:(id) sender{
        NSLog(@"till here");
    UIButton *button = (UIButton *)sender;
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:button.tag inSection:0];
    NSLog(@"indexpathc: %d",indexPath.row);

    [tableView beginUpdates];
    [tableView deleteRowsAtIndexPaths: [NSArray arrayWithObject:indexPath]
                     withRowAnimation: UITableViewRowAnimationFade];
    [_exceptions removeObjectAtIndex:indexPath.row];
    [tableView endUpdates];
}

It gives back the right index path in my log but it don't delete the row inside my tableview. Can anybody help me?

Kind regards

You also need to update the method numberOfRowsInSection . For example if you are keeping your elements in an array, delete one element from it and return updated array in numberOfRowsInSection .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM