【问题标题】:problems with animation when deleting the last row of a TableView in ios7在ios7中删除TableView的最后一行时出现动画问题
【发布时间】:2014-01-25 10:26:18
【问题描述】:

我在删除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);
            }
        }];
    }
}

我查看了所有能找到的答案,但都没有运气。我在 tableview 中的 numberOfSections 中返回 1,因为我只想要一个部分,并且我应该能够在一个部分中有 0 行,所以我认为这不是问题。

【问题讨论】:

  • 在扯掉头发之前,您应该尝试使用一些本机应用程序来重现这一点(Notes.app 是一个尝试的好地方)。如果您有一个小于屏幕高度的表格并且您删除了最后一行,则该行会停止,然后被删除。没有动画。我猜这是操作系统本身的一个错误。
  • 您正在设备上进行测试,对吧?
  • @GuyKogus 是的,设备和模拟器上的行为相同。
  • @mikekavouras,很有趣。时钟应用确实表现出相同的行为。这是操作系统本身的一个错误。太糟糕了。

标签: ios tableview editing


【解决方案1】:

是ios7的bug..tablerow动画坏了! 我的解决方法是在 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];
}

【讨论】:

  • 这个错误似乎已在 iOS 8 上得到修复,因此可能值得添加“
【解决方案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'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];
     }
}

【讨论】:

    【解决方案3】:

    我有同样的错误。我刚刚更改了 UITableViewRowAnimationMiddle 上的动画类型,它全部修复了。

    【讨论】:

    • UITableViewRowAnimationMiddle 确实适用于每一行,但它与UITableViewRowAnimationFade 的效果不同,因此它可能不是您想要的。
    【解决方案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];
                         }];
    }
    

    【讨论】:

      猜你喜欢
      • 2013-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-19
      • 1970-01-01
      • 1970-01-01
      • 2019-08-25
      相关资源
      最近更新 更多