Delete a UITableView row by click on custom UIButton

后端 未结 7 1054
梦谈多话
梦谈多话 2020-12-22 04:16

Basically I want to delete row on click event of a button which is a part of that row.
I can not use commit editing style because i want to perform unsubscribe and dele

7条回答
  •  别那么骄傲
    2020-12-22 04:49

    You should get the indexPath of the button that was tapped. And then delete the selected index paths using standard API call. Like so:

    - (IBAction) didTapSubscribeButton:(UIButton*)sender {
         UIView* candidate = button;
         while (![candidate isKindOfClass:[UITableViewCell class]]) {
            candidate = candidate.superview;
        }
        NSIndexPath* indexPathToDelete = [self.tableView indexPathForCell:candidate];
         [self.dataSourceArray removeObjectAtIndex:indexPath.row] //Considering that this is a single data source table view.
         NSArray *indexPaths = [NSArray alloc]initWithObjects:@[indexPath],nil];
         [self.tableView beginUpdates];
         tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
         [self.tableView endUpdates];
       }
    

提交回复
热议问题