Проблемы с анимацией при удалении последней строки TableView в ios7 - программирование

Проблемы с анимацией при удалении последней строки TableView в ios7

У меня возникают некоторые проблемы при удалении последней строки моего (только) раздела в tableView. Любая другая строка отлично работает, но если я удалю строку в нижней части моего tableView в любое время (не только в том случае, если она осталась последней), анимация будет очень странной и лагированной. Это просто не выглядит правильным. Я также заметил, что изменение типа анимации ничего не делает. Анимация всегда, когда строка скользит вверх и исчезает. Изменение его на UITableViewRowAnimationFade или другое не делает ничего.

Вот мой код

//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);
            }
        }];
    }
}

Я посмотрел на каждый ответ, который я могу найти без везения. Я возвращаю 1 в моем numberOfSections в tableView, потому что мне нужен только один раздел, и я должен иметь 0 строк в секции, поэтому я не думаю, что проблема.

4b9b3361

Ответ 1

Его ошибка ios7.. анимации tablerow сломаны! Мое исправление заключалось в fadeOut ячейке прямо перед 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];
}

Ответ 2

Вот решение, которое я нашел, чтобы работать хорошо, что основывается на ответе jaydee3:

- (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 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];
     }
}

Ответ 3

У меня была такая же ошибка. Я просто изменил тип анимации на UITableViewRowAnimationMiddle, и он исправил все это.

Ответ 4

Мое решение этой болезненной проблемы:)

- (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];
                     }];
}