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

查看:21
本文介绍了动画在 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*)sections withRowanimation(UITableViewRowAnimation) animation 但我还没有在网上找到合适的例子.

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.

因此您的插入内容可能如下所示:

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 要求您的数据模型反映您使用插入/删除方法所做的更改.因此,例如,如果在调用 insertSections:withRowAnimation:(通过它插入单个部分)之前,您的 numberOfSectionsInTableView: 方法返回 1,那么,在调用之后,它必须返回 2.否则会抛出异常.正是这种一致性的实施使您(再次,我认为)避免调用 reloadData - 整个 beginUpdates 正在重新加载必要的数据: endUpdates: 事务,以及您在该事务期间对模型所做的任何更新都必须与您的插入/删除调用一一匹配.

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.

像泥一样清澈?

更新

如果您正在编写显式动画,那么我会说您可以在完成处理程序"中执行此操作(或者直接为此编写动画),但这对您来说不可用.我认为您可以将按钮呈现代码包装在视图中自己的方法中,然后设置一个计时器在很短的时间后调用它,例如 0.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天全站免登陆