问题
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?
Thanks for your time, Dan.
回答1:
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.
回答2:
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];
}
来源:https://stackoverflow.com/questions/49182311/table-view-cell-background-goes-white-when-deleting-a-cell-ios