UITableView节页脚视图结束后的位置更新



在ios8上,我使用核心数据表视图控制器,删除行后,我的节页脚视图突然一直向下到UITableView的底部。当我滚动表视图时,页脚视图会回到原来的位置。如何解决这个问题,为什么会发生这种情况?

这是代码以防万一。

- (void)controller:(NSFetchedResultsController *)controller
   didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)indexPath
     forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)newIndexPath
{       
    if (!self.suspendAutomaticTrackingOfChangesInManagedObjectContext)
    {
        switch(type)
        {
            case NSFetchedResultsChangeInsert:
                [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
                break;
            case NSFetchedResultsChangeDelete:
                [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
                break;
            case NSFetchedResultsChangeUpdate:
                [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
                break;
            case NSFetchedResultsChangeMove:
                [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
                [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
                break;
        }
    }
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
    if (self.beganUpdates)
    {
       [self.tableView endUpdates];
    }
}

这是iOS 8的动画更改。您必须将表视图样式更改为"grouped",以防止节页脚移动到uitableview 的bootom

我刚刚遇到了同样的问题。当我使用insertRowsAtIndexPaths时。。页脚位于底部。还注意到,如果你在表视图上调用reloadData,它会解决问题,所以我这样做了:

[CATransaction begin];
[CATransaction setCompletionBlock: ^{
    // Code to be executed upon completion
    [tableView reloadData];
}];
[tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationLeft];
[CATransaction commit]; 

它所做的只是在插入动画完成时重新加载表。。。这不是一个真正的解决方案,更像是避免了问题,但它隐藏了问题,直到有人解释为什么会发生这种情况以及如何解决…

我遇到了同样的问题,并花了很多精力试图解决它。现在,终于!

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NATask *task = sections[indexPath.section][indexPath.row];
        [sections[indexPath.section] removeObjectAtIndex:indexPath.row];
        [appDelegate.managedObjectContext deleteObject:task];
        [CATransaction begin];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        UIView *footerView = [tableView footerViewForSection:indexPath.section];
        CABasicAnimation *animation = [[footerView.layer animationForKey:@"position"] mutableCopy];
        CGPoint fromValue = [(NSValue *)animation.fromValue CGPointValue];
        animation.toValue = [NSValue valueWithCGPoint:CGPointMake(0, fromValue.y - 44)];
        [footerView.layer removeAnimationForKey:@"position"];
        [footerView.layer addAnimation:animation forKey:@"position"];
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, .4 * NSEC_PER_SEC);
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            [tableView reloadData];
        });
        [CATransaction commit];
    }
}

当然,这不是最理想的解决方案,但它是有效的!我已经花了将近两个星期的时间来解决这个问题,我希望至少对某人有用。

我遇到了同样的问题。到目前为止,没有一个答案确切地告诉我我在寻找什么。将表视图设置为"grouped"不是我想要的行为,其他解决方案仍然导致页脚短暂地位于表视图的底部,直到调用reloadData,他们突然将其弹回正确的位置。

下面是我的解决方案,它顺利地动画到它应该在哪里。

NSIndexPath* iPath = [NSIndexPath indexPathForRow:_lineItemArray.count-1 inSection:0];
// Calculate the Y position of the bottom of the footer within the table view frame.
CGFloat footerBaseY = (_footer.y + _footer.height) - _sTableView.contentOffset.y;
// Calculate the Y position of the bottom taking into account the bottom content inset.
CGFloat tableViewContentBottomY = _sTableView.height - _sTableView.contentInset.bottom;
if (footerBaseY < tableViewContentBottomY) {
    [_sTableView insertRowsAtIndexPaths:@[iPath] withRowAnimation:UITableViewRowAnimationBottom];
    [UIView animateWithDuration:0.3
                     animations:^{
                         CGFloat y = _lineItemArray.count * [_sTableView rectForRowAtIndexPath:iPath].size.height;
                         CGFloat newBaseY = (y + _footer.height) - _sTableView.contentOffset.y;
                         if (newBaseY < tableViewContentBottomY) {
                             _footer.y = y;
                         } else {
                             // The footer is within a cell's height away from the bottom.  Subtract the diff from the new y.
                             _footer.y = y - (newBaseY - tableViewContentBottomY);
                         }
                     } completion:^(BOOL finished) {
                         // This ensures that the bottom row animates up if the footer was within a cell's height away from the bottom.
                         [_sTableView scrollToRowAtIndexPath:iPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
                     }];
} else {
    // The footer was already at the bottom of the vew so I'm animating the added row up.
    [_sTableView insertRowsAtIndexPaths:@[iPath] withRowAnimation:UITableViewRowAnimationBottom];
    [_sTableView scrollToRowAtIndexPath:iPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}

我仍然需要部分页眉和页脚视图具有粘性。因此,我没有将表视图样式更改为Grouped,而是在删除行后重新加载Section。。。

最新更新