Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
350 views
in Technique[技术] by (71.8m points)

ios - UITableViewCell custom editingAccessoryView - not properly dismissed

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 it.

When I scroll or select another row in the table view, my custom editing accessory is not dismissed. With the standard editing accessory (the delete button), a touch anywhere else on the table is captured and used to remove the delete accessory view - you can see this yourself on the built in Notes application, for instance, or in any other place with a standard editing accessory view.

This must be because I am returning UITableViewEditingStyleNone when I am in swipe-to-delete mode. However, if I return any other mode then my custom editing accessory is not displayed.

How can I get back the functionality of the standard editing style, where a touch anywhere on the table view dismisses the editing accessory?

The cell is not subclassed, but it is loaded from a nib file with a custom layout. The editing accessory view is part of the nib file and connected via the editingAccessoryView outlet.

I have managed to halfway achieve the effect I want by storing the index path of a swipe-to-edited row and setting that cell out of editing mode if another row is selected or scrolling begins on the table. However, I'd like to do it properly.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...