custom editingAccessoryView not working

前端 未结 1 1693
故里飘歌
故里飘歌 2020-12-14 13:02

I have the following code for a UITableView with custom cell:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)in         


        
相关标签:
1条回答
  • 2020-12-14 13:39

    The editing accessory view is shown when the cell enters editing mode. It does seem a little bit too hard to actually get this working, but I have managed it:

    To get this to show both when entering edit mode for the whole table, and when swiping an individual row, I have implemented the following in my UITableViewController subclass:

    - (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    
        if (editing)
            self.editingFromEditButton = YES;
        [super setEditing:(BOOL)editing animated:(BOOL)animated];
        self.editingFromEditButton = NO;
        // Other code you may want at this point...
    }
    

    editingFromEditButton is a BOOL property of the subclass. This method is called when the standard "Edit" button is pressed. It is used in the following method which prevents the standard delete button showing:

    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (self.editingFromEditButton)
            return UITableViewCellEditingStyleNone;
    
        // Otherwise, we are at swipe to delete
        [[tableView cellForRowAtIndexPath:indexPath] setEditing:YES animated:YES];
        return UITableViewCellEditingStyleNone;
    } 
    

    If the whole table view is being set to editing mode then each cell will also be sent the setEditing message. If we have swiped a single row, then we need to force that cell into editing mode, and then return the UITableViewCellEditingStyleNone style to prevent the standard delete button from appearing.

    Then, to dismiss the custom editing accessory, you also need the following code:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    {
        // Cancel the delete button if we are in swipe to edit mode
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        if (cell.editing && !self.editing)
        {
            [cell setEditing:NO animated:YES];
            return;
        }
    
        // Your standard code for when the row really is selected...
    }
    
    0 讨论(0)
提交回复
热议问题