Question

I'm having some issues when deleting the last row of my (only) section in my tableView. Any other row works fine, but if I delete the row at the bottom of my tableView at any time (not just when its the last one left) the animation is very strange and laggy. It just doesn't look right. I've also noticed that changing the animation type doesn't do anything. The animation is always when the row slides up to the top and disappears. Changing it to UITableViewRowAnimationFade or another doesn't do anything.

Here is my code

//For the edit barButtonItem in my storyboard
- (IBAction)editButtonPressed:(id)sender {
    //enter editing mode
    if ([self.editButton.title isEqualToString:@"Edit"]) {
        [self setEditing:YES animated:YES];
        self.editButton.title = @"Done";
    } else {
        [self setEditing:NO animated:YES];
        self.editButton.title = @"Edit";
    }
}

//Editing the tableView. The user can only delete rows, not add any
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    // If row is deleted, remove it from the list.
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //Handle the data source and backend first, then the tableView row
        PFRelation *hasFavorites = [self.currentUser relationforKey:@"hasFavorites"];
        [hasFavorites removeObject:[self.favorites objectAtIndex:indexPath.row]];

        [self.favorites removeObjectAtIndex:indexPath.row];

        //It is set to fade here, but it only ever does the top animation
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

        //save to the backend
        [self.currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (!error) {

            } else {
                NSLog(@"%@ %@", error, error.userInfo);
            }
        }];
    }
}

I've looked at every answer I can find with no luck. I return 1 in my numberOfSections in tableview because I only ever want one section, and I should be able to have 0 rows in a section, so I don't think that's the problem.

Was it helpful?

Solution

Its a bug of ios7.. the tablerow animations are broken! My fix was to fadeOut the cell right before the tableViewRowAnimation..

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // hide cell, because animations are broken on ios7
    double iosVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
    if (iosVersion >= 7.0 && iosVersion <= 8.0) {
        [tableView cellForRowAtIndexPath:indexPath].alpha = 0.0;
    }

    [tableView deleteRowsAtIndexPaths:@[indexPath]
                     withRowAnimation:UITableViewRowAnimationMiddle];
}

OTHER TIPS

Here is a solution I found to work well that builds upon jaydee3's answer:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{ 
     if (editingStyle == UITableViewCellEditingStyleDelete) {
          // hide cell, because animations are broken on ios7
          double iosVersion = [[[UIDevice currentDevice] systemVersion] floatValue];

          if (iosVersion >= 7.0 && iosVersion <= 8.0) {
              // Animating the cell's alpha change gives it a smooth transition
              // Durations > .17 show the glitch on iPad, phone looks nice up to 0.3
              [UIView animateWithDuration:0.17 animations:^(void) {
                  [tableView cellForRowAtIndexPath:indexPath].alpha = 0.0;
              }];
          }
          NSArray *deleteIndexPaths = [[NSArray alloc] initWithObjects:indexPath, nil];

          // UITableViewRowAnimationFade looks nice with the animation imho
          [tableView deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade];
     }
}

I had the same bug. I just changed the animation type on UITableViewRowAnimationMiddle, and it fixed it all.

My solution to this painful issue :)

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

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [UIView animateWithDuration:0.25f
                     animations:^{

                         UIScrollView *internalScrollView = (UIScrollView*)cell.contentView.superview;
                         if([internalScrollView isKindOfClass:[UIScrollView class]]){

                             [internalScrollView setContentOffset:CGPointZero animated:YES];
                         }

                         cell.center = CGPointMake(cell.center.x - cell.bounds.size.width, cell.center.y);

                     } completion:^(BOOL finished) {

                         [self.rowModels removeObjectAtIndex:indexPath.row];

                         [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
                     }];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top