Delete a UITableView row by click on custom UIButton

后端 未结 7 1007
梦谈多话
梦谈多话 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:51

    a. Add the target and callback to the button (I presume you already did that)

    b. In the callback function, loop over the superviews to find the UITableViewCell and then find the index of it, like so:

    -(void)deleteButtonPressed:(UIButton*)button {
        UIView* candidate = button;
        while (![candidate isKindOfClass:[UITableViewCell class]]) {
            candidate = candidate.superview;
        }
        NSIndexPath* indexPathToDelete = [self.tableView indexPathForCell:cell];
        ...
    

    c. Update your model first (very important)

    [self.dataArrayThatFeedsTableView removeObjectAtIndex:indexPathToDelete.item]
    

    d. Call the delete row

    [self.tableView deleteItemsAtIndexPaths:@[indexPathToDelete]];
    
    0 讨论(0)
提交回复
热议问题