-->

UITableViewRowAnimationFade Not Working

2020-07-17 06:43发布

问题:

So, this issue follows on from a previous issue, but I decided to post a new question to keep things relevant and tidy.

Basically, when the following piece of code is called, there is no difference between UITableViewRowAnimationFade and UITableViewRowAnimationNone:

- (void) setEditing:(BOOL)editing animated:(BOOL)animated
{

    [tvController.tableView beginUpdates];

    if (editing == YES) {
        [tvController.tableView deleteRowsAtIndexPaths:[settingsArray objectAtIndex:0] withRowAnimation:UITableViewRowAnimationFade];

    }else {

        UITableViewRowAnimation animation = animated ? UITableViewRowAnimationFade : UITableViewRowAnimationNone;
        [tvController.tableView reloadRowsAtIndexPaths:[settingsArray objectAtIndex:0] withRowAnimation:animation];

        [tvController.tableView reloadSectionIndexTitles];

        self.navigationItem.hidesBackButton = editing;
    }
    [tvController.tableView endUpdates];
}

Greatly appreciate any help. It still enters editing mode, but does not animate into it, depsite YES being passed into animated.


EDIT: The animation works fine when I'm actually deleting things using the following code:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

        NSString *stuff = [documentsPath stringByAppendingPathComponent:@"stuff.plist"];
        BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:stuff];

        if (fileExists) {

            NSMutableDictionary *propertyList = [[NSMutableDictionary alloc] initWithContentsOfFile:enteredPlaces];
            [propertyList removeObjectForKey:[[settingsArray objectAtIndex:1] objectAtIndex:indexPath.row]];
            [propertyList writeToFile:stuff atomically:YES];
        }

        [[settingsArray objectAtIndex:1] removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];

    } 
}

It just doesn't work when the user presses the edit button and the table goes into editing mode, the tableview just snaps statically into edit mode.

回答1:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [db DeleteData:[NSString stringWithFormat:@"%d",indexPath.row]];
        [data removeObjectAtIndex:indexPath.row];

        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation: UITableViewRowAnimationFade];

    }   

}


回答2:

I have same scenario in my application, I was facing the similar issue. I guess below function will solve your issue :

- (void) setEditing:(BOOL)editing animated:(BOOL)animated
{

    [tvController.tableView setEditing:editing animated:animated]; //this LOC is added 

    [tvController.tableView beginUpdates];

    if (editing == YES) {
        [tvController.tableView deleteRowsAtIndexPaths:[settingsArray objectAtIndex:0] withRowAnimation:UITableViewRowAnimationFade];

}    else {

        UITableViewRowAnimation animation = animated ? UITableViewRowAnimationFade : UITableViewRowAnimationNone;
        [tvController.tableView reloadRowsAtIndexPaths:[settingsArray objectAtIndex:0] withRowAnimation:animation];

        [tvController.tableView reloadSectionIndexTitles];

        self.navigationItem.hidesBackButton = editing;
    }
        [tvController.tableView endUpdates];
}


-(void)deleteRecord:(NSInteger)recordNo:(NSInteger)sectionNo:(BOOL)isEditMode:(BOOL)isAnimate {

if(isEditMode){
    NSIndexPath *indexP=[NSIndexPath indexPathForRow:recordNo inSection:sectionNo];
    [tvController.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexP, nil] withRowAnimation:UITableViewRowAnimationLeft];
}
else {

    if(isAnimate)
        [tvController.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexP, nil] withRowAnimation:UITableViewRowAnimationFade];
    else
        [tvController.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexP, nil] withRowAnimation:UITableViewRowAnimationNone];

    [tvController.tableView reloadSectionIndexTitles];
    self.navigationItem.hidesBackButton = editing;

}


[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(reload) userInfo:nil repeats:NO];
}


 -(void)reload {
    [table reloadData];
}


回答3:

if the only thing that you are annoyed by is the fact that the delete doesn't fade, it is because you are anticipating something that the interface doesn't provide.

you mentioned that you know individual cells can be set to be non-editable, and this is the standard way to do this. the mechanism simply does not anticipate not using the UITableViewDataSource to provide the information about what to display (or, in your case, what is in the data being changed) simply by hitting the edit/done button.

by trying to combine the two, you are confusing the animation that is supposed to happen.

probably the best you can do is something like the following (and the animation duration and length could possibly be 0, since you're asking for separate animation in the tableView), which will cause your animation to occur after the animation that opens up editing mode.

- (void) setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];  // possibly not necessary depending upon your class hierarchy
    [tvController.tableView setEditing:editing animated:animated];
    [UIView animateWithDuration:0.25 delay:0.05 options:UIViewAnimationCurveLinear
                     animations:^{ 
                         [tvController.tableView beginUpdates];

                         if (editing == YES) {
                             [tvController.tableView deleteRowsAtIndexPaths:[settingsArray objectAtIndex:0] withRowAnimation:UITableViewRowAnimationFade];
                         } else {
                             UITableViewRowAnimation animation = animated ? UITableViewRowAnimationFade : UITableViewRowAnimationNone;
                             [tvController.tableView reloadRowsAtIndexPaths:[settingsArray objectAtIndex:0] withRowAnimation:animation];
                             [tvController.tableView reloadSectionIndexTitles];
                             self.navigationItem.hidesBackButton = editing;
                         }
                         [tvController.tableView endUpdates];
                     }
                     completion:nil];
}