在UITableVIew中插入新部分的动画 [英] Animate the insertion of a new section in UITableVIew

查看:126
本文介绍了在UITableVIew中插入新部分的动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have this button on my view, and when I press it I insert a new section in my table view ( I have a logical condition in my

-(NSInteger) numberOfSectionsInTableView:(UITableView*)tableView
{
    if (slide==TRUE) return 2;
    return 1;
}

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

And also in my -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath. My section is added as it should, but I have read somewhere that this can be animated, because when I press my button the section is added but with no animation. I think I should use this -(void)insertSections:(NSIndexSet*)sections withRowanimation(UITableViewRowAnimation) animation but I haven't found a proper example on the web.

推荐答案

UITableViewRowAnimation 是在UITableView.h顶部声明的枚举你也可以在 UITableView参考。它很小,所以我只是粘贴它!

UITableViewRowAnimation is an enum declared at the top of UITableView.h You can also view it in the UITableView reference. It's smallish, so I'll just paste it!

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;

基本上它告诉表格视图你想要在哪个方向动画输入/输出行/部分。一个小实验将证明每个的效果。

Basically it tells the table view from which direction you want the rows/sections to be animated in/out. A little experimenting will demonstrate the effect of each.

例如,插入一行 UITableViewRowAnimationTop 将触发一个动画从表格视图中最终目的地的空间正上方的空格中看到一行进入表格视图的印象。

For example, inserting a row with UITableViewRowAnimationTop will trigger an animation which gives the impression of a row coming into the table view from a space immediately above that of its final destination in the table view.

因此您的插入可能如下所示: / p>

So your insertion might look like:

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

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

And you will have to be sure that your delegate and data source provide info that is consistent with your assertion to insert sections/rows. From your question, it looks like you have done so.

编辑:

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

I don't think you have to call reloadData. UITableView requires that your data model reflect the changes you make with with the insert/delete methods. So that, for example, if, before a call to insertSections:withRowAnimation: (with which you are inserting a single section), your numberOfSectionsInTableView: method returned 1, then, after the call, it must return 2. To do otherwise throws an exception. It is this enforcement of consistency that allows you (again, I think) to avoid the call to reloadData - the necessary data is being reloaded by the whole beginUpdates: endUpdates: transaction, and any updates to the model you make during that transaction must match one-for-one your insert/delete calls.

清除泥浆?

更新

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

If you were programming explicit animations, then I would say that you could do it in the 'completion handler' (or just program the animation directly for that matter), but that is not available to you here. I think you can wrap the button-presenting code in its own method in the view, then set a timer to call it after a short amount of time, say, .2 seconds (you'll have to experiment to see what looks good). e.g.

[NSTimer scheduledTimerWithTimeInterval:.2 target:self selector:@selector(presentButton) userInfo:nil repeats:NO];

这应该可以解决问题。

这篇关于在UITableVIew中插入新部分的动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆