2014-10-07 95 views
18

在ios8我使用核心數據表視圖控制器,並刪除行後,我的部分頁腳視圖突然一路下降到底部的UITableView。當我滾動表格視圖時,頁腳視圖返回到它的位置。如何解決這個問題,爲什麼會發生這種情況?UITableView頁腳查看位置後endUpdates

這是代碼以防萬一。

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

看看這篇文章http://stackoverflow.com/questions/5740518/uitableview-footer-stop-from-浮動內容 – 2016-05-19 02:45:56

+0

我提交了一個[雷達](https://openradar.appspot.com/radar?id=6187705981468672)針對這個問題。如果你想修正這個問題,我建議你們都這樣做。 Dupes非常有說服力。 – gurooj 2016-09-17 00:00:35

回答

3

我剛剛遇到同樣的問題。當我使用insertRowsAtIndexPaths ..頁腳到底部。還注意到,如果你調用reloadData上表中查看,所以我這樣做會解決這個問題:

[CATransaction begin]; 
[CATransaction setCompletionBlock: ^{ 
    // Code to be executed upon completion 
    [tableView reloadData]; 
}]; 

[tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationLeft]; 
[CATransaction commit]; 

它所做的就是重新加載表時插入動畫完成... 這不是一個真正的解決方案,更喜歡避免這個問題,但它隱藏了問題,直到有人解釋爲什麼會發生,以及如何解決這個問題。...

+0

是的,你也可以這樣做,以避免這種情況(更好的然後重新加載數據,但它也很cheesy): [self.tableView setContentOffset:CGPointMake(self.tableView.contentOffset.x,self.tableView.contentOffset.y- 1)]; – t0a0 2014-10-07 19:30:04

11

這是一個iOS 8動畫更改。你必須改變你的桌面風格爲'分組',以保持部分頁腳不會移動到可用視圖的底部

+1

謝謝,這個解決方案爲我工作。你有鏈接,這是記錄在案? – 2015-03-06 14:33:27

+0

該解決方案聽起來更像是一種破解,而不是解決問題的恰當方式。 – RaffAl 2015-09-08 15:35:47

1

我遇到了同樣的問題,並花了很大的努力試圖解決它。現在,終於!

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

當然,這不是最理想的解決方案,但它的工作原理!我已經差不多兩週時間處理這個問題了,我希望至少對某人有用。

0

我遇到了同樣的問題。到目前爲止,沒有任何答案給了我我正在尋找的東西。將表視圖設置爲「分組」不是我想要的行爲,其他解決方案仍然導致腳註暫時位於表視圖的底部,直到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]; 
} 
0

我仍然需要部分頁眉和頁腳視圖粘滯。所以,而不是將tableview樣式更改爲分組,我只是刪除行後做了reloadsection ...