UITableView节尾查看endUpdates后的位置

在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的animation更改。 您必须将您的tableview样式更改为“分组”,以便使分节页脚不会移动到可用视图的自引

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

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

它只是在插入animation完成时重新加载表格…这不是一个真正的解决scheme,更像是避免了这个问题,但它隐藏了问题,直到有人解释为什么会发生,以及如何解决这个问题….

我遇到了同样的问题,并花费了大量的精力来解决这个问题。 现在,终于!

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

当然,这不是最理想的解决scheme,但它的工作原理! 我已经差不多两个星期的时间来解决这个问题了,我希望至less对某个人有用。

我遇到了同样的问题。 目前为止,没有任何答案给了我正在寻找的东西。 将表视图设置为“分组”不是我想要的行为,其他解决scheme仍然导致脚注暂时位于表视图的底部,直到reloadData被调用,然后它们突然将其捕捉回正确的位置。

下面是我的解决scheme,顺利animation到它应该在哪里。

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

我仍然需要部分页眉和页脚视图粘滞。 所以,而不是改变tableview风格分组,我只是做了reloadsection后删除行…