UITableViewCell custom editingAccessoryView - not properly dismissed

前端 未结 1 941
一个人的身影
一个人的身影 2020-12-17 07:24

I have implemented a custom editing accessory view as described in my answer to this question. For the most part it works very well, but I have noticed a small problem with

相关标签:
1条回答
  • 2020-12-17 07:59

    I was able to solve this, but sadly it requires additional legwork and isnt just as simple as setting a couple properties.

    In my

    - (UITableViewCellEditingStyle)tableView:(UITableView *)_tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
    

    method I return UITableViewCellEditingStyleNone so that my custom editingAccessoryView will show up. In this method I also do this:

    self.tableView.scrollEnabled = NO;
    if(self.editingPath)
    {
        [[tableView cellForRowAtIndexPath:editingPath] setEditing:NO animated:YES];
    }
    
    self.editingPath = indexPath;    
    for (UITableViewCell *cell in [tableView visibleCells])
    {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    

    This disables scrolling, then stores the indexPath we swiped on for later use. If you swipe another row, while editing a row, it will unedit the first row and edit the second row, which is how apple apps behave. I also set the cell selectionStyle on all visible cells to UITableViewCellSelectionStyleNone. This reduces the blue flicker when the user selects another cell while your currently editing one.

    Next we need to dismiss the accessoryView when another cell is tapped. To do that we implement this method:

    -(NSIndexPath *)tableView:(UITableView *)_tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    if(self.editingPath)
    {
        UITableViewCell *c = [tableView cellForRowAtIndexPath:self.editingPath];
        [c setEditing:NO animated:YES];
    
        self.tableView.scrollEnabled = YES;
        self.editingPath = nil;
        for (UITableViewCell *cell in [tableView visibleCells])
        {
            cell.selectionStyle = UITableViewCellSelectionStyleBlue;
        }
    
        return nil;
    }
    
    return indexPath;
    }
    

    What this does is when someone is about to click on a cell, if we are editing then unedit that cell and return nothing.

    also for

    -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
    

    I return YES, to enable editing on the cells I want the user to be able to delete.

    0 讨论(0)
提交回复
热议问题