3

I have an iOS app with a UITableView, I have noticed that the cell background colour flashes white when the user selects the Delete button.

In the editActionsForRowAtIndexPath method, I have created two cell buttons: Edit and Delete. The first button's style is set to UITableViewRowActionStyleNormal. however the second button's style is set to UITableViewRowActionStyleDestructive - I have noticed that this issue only occurs when then style is set to destructive. Does anyone know why this is happening?

Here is the method I am using to set the cell action buttons:

-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // Create the table view cell edit buttons.
    UITableViewRowAction *editButton = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"Edit" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        
        // Edit the selected action.
        [self editAction:indexPath];
    }];
    editButton.backgroundColor = [UIColor blueColor];
    
    UITableViewRowAction *deleteButton = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        
        // Delete the selected action.
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }];
    
    return @[deleteButton, editButton];
}

The colour of the cell is normal when the user is scrolling, tapping it or when he/she selects the Edit button, but when they select the Delete button, the cell turns white as the deletion animation is occurring.

How can I fix this?

halfer
  • 19,824
  • 17
  • 99
  • 186
Supertecnoboff
  • 6,406
  • 11
  • 57
  • 98
  • Possible duplicate of [iOS 11/Xcode 9: UITableViewCell white background flickers on delete](https://stackoverflow.com/questions/46477291/ios-11-xcode-9-uitableviewcell-white-background-flickers-on-delete) – Kamil Harasimowicz Mar 30 '18 at 11:22

2 Answers2

2

It turns out the issue I am experiencing, is due to an iOS bug. I found a solution here: https://stackoverflow.com/a/46649768/1598906

[[UITableViewCell appearance] setBackgroundColor:[UIColor clearColor]];

The above code is set in the App Delegate, it set the background color to clear, thus removing the white background view.

Supertecnoboff
  • 6,406
  • 11
  • 57
  • 98
0

Before calling the deleteRowsAtIndexPaths method, you need to remove the object from the data source;

Replace this:

UITableViewRowAction *deleteButton = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {

        // Delete the selected action.
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }];

with something like this:

UITableViewRowAction *deleteButton = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {

        // Delete the selected action.
        [self deleteObjectAtIndexPath:indexPath];
    }];

and the method to delete:

- (void)deleteObjectAtIndexPath:(NSIndexPath *)indexPath {
    // remove object from data source. I assume that you have an array dataSource, or change it according with your data source
    [self.dataSource removeObjectAtIndex:(indexPath.row)];

    [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
Mihai D.
  • 105
  • 1
  • 4