在UITableVIew中插入新部分的动画

Cos*_*min 12 animation uitableview ios

我在我的视图上有这个按钮,当我按下它时,我在表格视图中插入一个新的部分(我的逻辑状态在我的

-(NSInteger) numberOfSectionsInTableView:(UITableView*)tableView
{
    if (slide==TRUE) return 2;
    return 1;
}
Run Code Online (Sandbox Code Playgroud)

而且在我的-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath.我的部分是按照它应该添加的,但是我已经读过一些可以动画的地方,因为当我按下我的按钮时,该部分被添加但没有动画.我想我应该使用它,-(void)insertSections:(NSIndexSet*)sections withRowanimation(UITableViewRowAnimation) animation但我没有在网上找到一个合适的例子.

Sam*_*Sam 31

使用以下API(强调最后两个),使用动画将行和节操作到UITableView中非常简单:

更新数据模型

您应该在beginUpdatesendUpdates方法之间的任何位置更新数据模型.如果您在插入或删除方法之前或之后更新数据模型,只要在beginUpdatesendUpdates方法之间进行更新就没有关系.

不要为要插入的新部分插入行

使用该insertSections:withRowAnimation:方法添加新节时,无需调用insertRowsAtIndexPaths:withRowAnimation:即可向其中添加行.该insertRowsAtIndexPaths:withRowAnimation:方法仅用于动画现有的部分更改.同样,deleteRowsAtIndexPaths:withRowAnimation:使用时删除部分也不需要调用deleteSections:withRowAnimation:.

关于删除和插入:

我通常在单独的调用中执行这些操作,但您可以同时插入和删除.请注意,UITableView将首先尝试删除行/节,然后尝试插入它们,无论您在代码中执行此操作的顺序如何.beginUpdates: endUpdates

讨论部分deleteRowsAtIndexPaths:withRowAnimation:

请注意在beginUpdates和endUpdates方法定义的动画块中调用此方法时的行为.UITableView延迟任何行或节的插入,直到它处理了行或节的删除.无论插入和删除方法调用的顺序如何,都会发生这种情况.这与在可变数组中插入或移除项目不同,其中操作可以影响用于连续插入或移除操作的数组索引.有关此主题的更多信息,请参阅"表格视图编程指南"中的"批量插入和删除行和节".

插入部分的示例:

int indexOfNewSection = 4; // TODO: change to meaningful value
[self.tableview beginUpdates];
[self.tableview insertSections:[NSIndexSet indexSetWithIndex:indexOfNewSection]
              withRowAnimation:UITableViewRowAnimationAutomatic];

// Update data model
[self.sections insertObject:sectionObj atIndex:indexOfNewSection];

[self.tableview endUpdates];
Run Code Online (Sandbox Code Playgroud)


QED*_*QED 18

UITableViewRowAnimation是在UITableView.h顶部声明的枚举.您还可以在UITableView参考中查看它.它很小,所以我只是粘贴它!

typedef enum {
    UITableViewRowAnimationFade,
    UITableViewRowAnimationRight,           // slide in from right (or out to right)
    UITableViewRowAnimationLeft,
    UITableViewRowAnimationTop,
    UITableViewRowAnimationBottom,
    UITableViewRowAnimationNone,            // available in iOS 3.0
    UITableViewRowAnimationMiddle,          // available in iOS 3.2.  attempts to keep cell centered in the space it will/did occupy
    UITableViewRowAnimationAutomatic = 100  // available in iOS 5.0.  chooses an appropriate animation style for you
} UITableViewRowAnimation;
Run Code Online (Sandbox Code Playgroud)

基本上,它告诉表视图,您希望在哪个方向上对行/节进行动画处理.一个小小的实验将证明每个的效果.

例如,插入一行UITableViewRowAnimationTop将触发一个动画,该动画给出一个行的印象,该行从表视图中最终目标的空间正上方的空间进入表视图.

所以你的插入可能如下所示:

-(void)sliderValueChanged:(id)slider {
   slide = slider.on;

   [tableView beginUpdates];

   if (slider.on) {
      [tableView insertSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationTop];
      // TODO: update data model by inserting new section
   } else {
      [tableView deleteSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationTop];
      // TODO: update data model by removing approprite section
   }

   [tableView endUpdates];
}
Run Code Online (Sandbox Code Playgroud)

并且您必须确保您的委托和数据源提供与插入节/行的断言一致的信息.从您的问题来看,您似乎已经这样做了.

EDITS:

认为你不得打电话reloadData.UITableView要求您的数据模型反映您使用插入/删除方法所做的更改.因此,例如,如果在调用insertSections:withRowAnimation:(用于插入单个节)之前,numberOfSectionsInTableView:方法返回1,则在调用之后,它必须返回2.否则抛出异常.正是这种一致性强制允许您(我认为)再次避免调用reloadData- 整个beginUpdates: endUpdates:事务正在重新加载必要的数据,并且您在该事务期间对模型所做的任何更新都必须匹配一对一你的插入/删除电话.

像泥一样清楚?

更新

如果您正在编写显式动画,那么我会说您可以在"完成处理程序"中执行此操作(或者直接针对该问题直接编写动画),但这不适用于您.我想你可以在视图中用自己的方法包装按钮呈现代码,然后设置一个计时器,在很短的时间后调用它,比如说.2秒(你必须尝试看看看起来不错的东西) .例如

[NSTimer scheduledTimerWithTimeInterval:.2 target:self selector:@selector(presentButton) userInfo:nil repeats:NO];
Run Code Online (Sandbox Code Playgroud)

这应该够了吧.