UITableView使用reloadData的几种动画方法

  1. 其实,这是很简单的:
    [_tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
    从 调用表视图要求其数据源 新的cell的指定部分。表视图的动画 插入新的cell,因为它的动画老cell出来的。
  2. 我相信你可以更新你的数据结构,那么:
[tableView beginUpdates];
[tableView deleteSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:YES];
[tableView insertSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:YES];
[tableView endUpdates];

此外,“withRowAnimation”不完全是一个布尔值,而是一个动画样式:
UITableViewRowAnimationFade,
UITableViewRowAnimationRight,
UITableViewRowAnimationLeft,
UITableViewRowAnimationTop,
UITableViewRowAnimationBottom,
UITableViewRowAnimationNone,
UITableViewRowAnimationMiddle

  1. 您可能希望
/* Animate the table view reload */
[UIView transitionWithView: self.tableView
     duration: 0.35f
     options: UIViewAnimationOptionTransitionCrossDissolve
    animations: ^(void)
 {
 [self.tableView reloadData];
 }
    completion: ^(BOOL isFinished)
 {
 /* TODO: Whatever you want here */
 }];
  1. 有更多的CATransition类。 它并不局限于淡出,但可以做以及.. 例如: (不要忘了导入QuartzCore)
CATransition *transition = [CATransition animation];
transition.type = kCATransitionPush;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.fillMode = kCAFillModeForwards;
transition.duration = 0.5;
transition.subtype = kCATransitionFromBottom;
[[self.tableView layer] addAnimation:transition forKey:@"UITableViewReloadDataAnimationKey"];

改变type以满足您的需求 CodeGo.net,像kCATransitionFade等等 参考CATransition

  1. 处理这个问题的方法是告诉的tableView删除和添加行和段与insertRowsAtIndexPaths:withRowAnimation:,deleteRowsAtIndexPaths:withRowAnimation:,insertSections:withRowAnimation:和deleteSections:withRowAnimation:UITableView中的方法。当调用表将动画/缩小您要求的项目,然后调用本身reloadData这样你就可以这样的动画更新后的状态。这部分是很重要的-如果你的动画了一切,但不改变由表中的数据源返回的数据,该行会后,再次出现 所以,你的应用程序的线程将是:[self setTableIsInSecondState:YES];[myTable deleteSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:YES]];只要你的表的检查返回正确的新的章节和行[self tableIsInSecondState](或其他),这将实现你要找的效果。

  2. 所有这些答案,你只用1节一个UITableView。 为了准确地处理,你有超过1的情况

NSRange range = NSMakeRange(0, (myTableView.numberOfSections - 1));
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:range];
[myTableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];

(注:你应该确保你有超过0的部分!) 另一个要注意的是,你可能会遇到一个NSInternalInconsistencyException如果您尝试更新此代码数据源。如果是这种情况,则逻辑与此类似:
int sectionNumber = 0; //Note that your section may be different
int nextIndex = [currentItems count]; //starting index of newly added items
[myTableView beginUpdates];
for (NSObject *item in itemsToAdd) {
//Add the item to the data source
[currentItems addObject:item];
//Add the item to the table view
NSIndexPath *path = [NSIndexPath indexPathForRow:nextIndex++ inSection:sectionNumber];
[myTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:path] withRowAnimation:UITableViewRowAnimationAutomatic];
}
[myTableView endUpdates];

  1. 这里是真正典型的代码扩展,在本例中两行,在当今时代一个完整的例子(这是2013年11月!) (需要注意的是,你可以做任何数量的部分,行,混合输入/输出,各尽所能,使用此代码。) 需要注意的是facebookRowsExpanded是一个类变量,你必须有:
if ( [theCommand isEqualToString:@"fbexpander"] )
// user has clicked on the row that means "expand some lines"...
{
NSLog(@"expander button clicked......");
[tableView deselectRowAtIndexPath:indexPath animated:NO];
NSArray *deleteIndexPaths;
NSArray *insertIndexPaths;
facebookRowsExpanded = !facebookRowsExpanded;
// you must do that BEFORE, not AFTER the animation:
if ( !facebookRowsExpanded ) // ie, it was just true, is now false
 {
 deleteIndexPaths = [NSArray arrayWithObjects:
   [NSIndexPath indexPathForRow:2 inSection:0],
   [NSIndexPath indexPathForRow:3 inSection:0],
    nil];
 [tableView beginUpdates];
 [tableView
  deleteRowsAtIndexPaths:deleteIndexPaths
  withRowAnimation: UITableViewRowAnimationMiddle];
 [tableView endUpdates];
 }
else
 {
 insertIndexPaths = [NSArray arrayWithObjects:
   [NSIndexPath indexPathForRow:2 inSection:0],
   [NSIndexPath indexPathForRow:3 inSection:0],
    nil];
 [tableView beginUpdates];
 [tableView
  insertRowsAtIndexPaths:insertIndexPaths
  withRowAnimation: UITableViewRowAnimationMiddle];
 [tableView endUpdates];
 }
// DO NOT do this at the end: [_superTableView reloadData];
return;
}

因为在这样的代码索引路径,设置你想要的任何东西。 下面是关键的部分:你的代码numberOfRowsInSection facebookRowsExpanded (它会像“如果facebookRowsExpanded回报7,否则返回5”) 同理:你的代码的cellForRowAtIndexPath facebookRowsExpanded。 (它返回正确的行,这取决于您是否正在展开。) 需要注意的是UITableViewRowAnimationMiddle几乎可以肯定它看起来最好的两个方面,它看起来最现代化的最佳选择。 (下面是别人试试… UITableViewRowAnimationFade, UITableViewRowAnimationRight, UITableViewRowAnimationLeft, UITableViewRowAnimationTop, UITableViewRowAnimationBottom, UITableViewRowAnimationNone, UITableViewRowAnimationMiddle, UITableViewRowAnimationAutomatic) 下面是numberOfRowsInSection的实际例子。这是一个相对简单的例子。 请注意,在这个例子中,我们碰巧被从一个plist中采取的行。 plist中有7行(这样,当在我们的例子扩充的)。所以很简单,它返回,你可以看到7时facebookRowsExpanded是真实的,5时facebookRowsExpanded是假的。 请注意,在上面的扩展代码,它在那里你翻转布尔绝对是至关重要的:它必须像上面的例子代码中完成。

-(NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section
 {
 if ( section == 0 ) // our function buttons
  {
  if ( facebookRowsExpanded )
   return menuRowsFromPlist.count;
  else
   return menuRowsFromPlist.count - 2;
  }
 if ( section == 1 ) // some other part of the table..
  return .. etc
 return 0;
 }

最后的的cellForRowAtIndexPath,它看起来像这样:
- (UITableViewCell*)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ( facebookRowsExpanded )
for example, for indexPath.row 6, you’d return item 6
if ( ! facebookRowsExpanded )
for example, for indexPath.row 6, you’d return item 4
}
请注意,最简单的方法是:有两个不同的数组(或NSDictionaries,或plists,或其他),1的展开状态,一个用于折叠状态。 或者你可以遍历并“跳过”的倒塌物品。 (这可能是情况最绝对通用的解决方案。) 在情况下,你可以KVO,使其更容易,在任何情况下,它必须返回正确的行“在那(基于facebookRowsExpanded)。 当然,如果它只是一个非常小的简单的表,只是代码它的手,也许与facebookRowsExpanded的每一种情况下的开关。 再次,这是至关重要的的cellForRowAtIndexPath-和numberOfRowsInSection-返回完全正确的价值观“在这个取决于全局facebookRowsExpanded的价值,并再次,它翻转了布尔在正确的点在动画代码非常关键的(如上图所示)。 。 希望它可以帮助 我忘了你可能要更改的“扩大”行本身。即实际行会点击透露其他两行。 (你可能会说,该三角形,或什么的,但你想改变实际的行,在你动画的两个新行的显示。) 下面是该示例代码:

{
NSLog(@"expander button......");
[tableView deselectRowAtIndexPath:indexPath animated:NO];
NSArray *expansionItemsIndexPaths = [NSArray arrayWithObjects:
       [NSIndexPath indexPathForRow:2 inSection:0],
       [NSIndexPath indexPathForRow:3 inSection:0],
       nil];
NSArray *swapItemIndexPath = [NSArray arrayWithObjects:
       [NSIndexPath indexPathForRow:1 inSection:0],
       nil];
facebookRowsExpanded = !facebookRowsExpanded;
if ( !facebookRowsExpanded ) // ie, it was just true
 {
 [tableView beginUpdates];
 [tableView
  deleteRowsAtIndexPaths: expansionItemsIndexPaths
  withRowAnimation: UITableViewRowAnimationMiddle];
 [tableView endUpdates];
 [tableView reloadRowsAtIndexPaths:swapItemIndexPath
  withRowAnimation: UITableViewRowAnimationFade];
 // YOU MAY PREFER: UITableViewRowAnimationNone
 }
else
 {
 [tableView beginUpdates];
 [tableView
  insertRowsAtIndexPaths: expansionItemsIndexPaths
  withRowAnimation: UITableViewRowAnimationMiddle];
 [tableView endUpdates];
 [tableView reloadRowsAtIndexPaths:swapItemIndexPath
  withRowAnimation: UITableViewRowAnimationFade];
 // YOU MAY PREFER: UITableViewRowAnimationNone
 }
return;
}

秘诀就是简单reloadRowsAtIndexPaths与问题只是一个排,而且,这样做只是“后,”你揭示或崩溃。我不知道UITableViewRowAnimationFade或UITableViewRowAnimationNone是否给出了最现代的外观,尝试为您的项目两个。希望它可以帮助

猜你喜欢

转载自blog.csdn.net/czl0325/article/details/46861749