UITableView上的animationreloadData

你将如何动漫- reloadDataUITableView - reloadData ? 数据源是在UIFetchedResultsController所以我不能玩– insertSections:withRowAnimation: – deleteSections:withRowAnimation:包裹在– beginUpdates– endUpdates

编辑:我想调用NSFetchedResultsController重新获取后- reloadData

我做了类别的方法。

 - (void)reloadData:(BOOL)animated { [self reloadData]; if (animated) { CATransition *animation = [CATransition animation]; [animation setType:kCATransitionPush]; [animation setSubtype:kCATransitionFromBottom]; [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]]; [animation setFillMode:kCAFillModeBoth]; [animation setDuration:.3]; [[self layer] addAnimation:animation forKey:@"UITableViewReloadDataAnimationKey"]; } } 

您可以使用以下方法制作reLoadData的基本animation:

 // Reload table with a slight animation [UIView transitionWithView:tableViewReference duration:0.5f options:UIViewAnimationOptionTransitionCrossDissolve animations:^(void) { [tableViewReference reloadData]; } completion:NULL]; 

当你想用animation重新加载整个表格时,你可以简单地调用这一行:

 NSRange range = NSMakeRange(0, [self numberOfSectionsInTableView:self.tableView]); NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange:range]; [self.tableView reloadSections:sections withRowAnimation:UITableViewRowAnimationFade]; 

你不能animationreloadData 。 你将不得不使用表视图的insert...delete...move...reloadRows...方法,使其animation。

使用NSFetchedResultsController时,这非常简单。 NSFetchedResultsControllerDelegate文档包含一组示例方法,您只需修改自己的代码:

 - (void)controllerWillChangeContent:(NSFetchedResultsController *)controller { [self.tableView beginUpdates]; } - (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type { switch(type) { case NSFetchedResultsChangeInsert: [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade]; break; case NSFetchedResultsChangeDelete: [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade]; break; } } - (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath { UITableView *tableView = self.tableView; switch(type) { case NSFetchedResultsChangeInsert: [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; break; case NSFetchedResultsChangeDelete: [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; break; case NSFetchedResultsChangeUpdate: [self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath]; break; case NSFetchedResultsChangeMove: [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; break; } } - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { [self.tableView endUpdates]; } 

我从这个链接创build了一个基于解决scheme的UITableView类别方法。

它应该重新加载所有的表格部分。 您可以使用UITableViewRowAnimation选项来播放不同的animation效果。

 - (void)reloadData:(BOOL)animated { [self reloadData]; if (animated) { [self reloadSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, self.numberOfSections)] withRowAnimation:UITableViewRowAnimationBottom]; } } 
  typedef enum { UITableViewRowAnimationFade, UITableViewRowAnimationRight, UITableViewRowAnimationLeft, UITableViewRowAnimationTop, UITableViewRowAnimationBottom, UITableViewRowAnimationNone, UITableViewRowAnimationMiddle, UITableViewRowAnimationAutomatic = 100 } UITableViewRowAnimation; 

和方法:

  [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade]; 

您可能想要使用:

Objective-C的

 /* Animate the table view reload */ [UIView transitionWithView:self.tableView duration:0.35f options:UIViewAnimationOptionTransitionCrossDissolve animations:^(void) { [self.tableView reloadData]; } completion:nil]; 

迅速

 UIView.transitionWithView(tableView, duration:0.35, options:.TransitionCrossDissolve, animations: { () -> Void in self.tableView.reloadData() }, completion: nil); 

animation选项:

TransitionNone TransitionFlipFromLeft TransitionFlipFromRight TransitionCurlUp TransitionCurlDown TransitionCrossDissolve TransitionFlipFromTop TransitionFlipFromBottom

参考