4

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.

Community
  • 1
  • 1
prince
  • 506
  • 6
  • 18

3 Answers3

0

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];
}
Kuldeep
  • 2,589
  • 1
  • 18
  • 28
  • The issue is not when deleting things, but rather when I set the editing mode to YES. See my edit – prince Jul 07 '12 at 09:17
  • Any specific reason why you override setEditing method ? – Kuldeep Jul 07 '12 at 10:06
  • You just need to setEditing mode to YES while editButton clicked and NO while done Button clicked. Let me know if it make sense. – Kuldeep Jul 07 '12 at 10:12
  • I do it so that I can remove the first section of the table, because the first section is not editable and so I do not want it to be on the screen, I know that I can individually set the cells to be un-editable, but I'd rather see it this way. – prince Jul 07 '12 at 11:00
  • I've added that line, but I still have the same result. – prince Jul 07 '12 at 12:18
0
- (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];

    }   

}
iAndroid
  • 951
  • 7
  • 18
  • The issue is not when deleting things, but rather when I set the editing mode to YES. See my edit – prince Jul 07 '12 at 09:17
0

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];
}
john.k.doe
  • 7,533
  • 2
  • 37
  • 64