我的 View 上有此按钮,当我按此按钮时,我在表格 View 中插入了一个新部分(我的逻辑条件是
-(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
,但在网上找不到合适的示例。 最佳答案
UITableViewRowAnimation
是在UITableView.h顶部声明的枚举。您也可以在UITableView reference中查看它。它很小,所以我将其粘贴!
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;
基本上,它告诉表格 View 您希望从哪个方向对行/节进行动画进/出。进行一些实验将证明每种方法的效果。
例如,插入带有
UITableViewRowAnimationTop
的行将触发一个动画,该动画使行的印象从表 View 中最终目标位置的紧上方开始进入表 View 。因此,您的插入内容可能如下所示:
-(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];
}
而且,您必须确保您的委托(delegate)人和数据源提供的信息与您插入断言/行的声明一致。从您的问题来看,您似乎已经这样做了。
编辑:
我认为您不必致电
reloadData
。 UITableView
要求您的数据模型反射(reflect)您使用插入/删除方法所做的更改。因此,例如,如果在调用insertSections:withRowAnimation:
(插入单个节)之前,您的numberOfSectionsInTableView:
方法返回1,那么在调用之后,它必须返回2。否则,将引发异常。正是这种一致性的实现,使您(再次,我认为)可以避免调用reloadData
-整个beginUpdates:
endUpdates:
事务将重新加载必要的数据,并且您在该事务期间对模型进行的任何更新都必须符合以下条件:一对一的插入/删除调用。像泥一样清澈?
更新
如果您正在编写显式动画,那么我想说您可以在“完成处理程序”中完成此操作(或者直接为此编写动画),但是此处不提供此功能。我认为您可以在 View 中将按钮呈现代码包装在其自己的方法中,然后设置一个计时器以在短时间(例如0.2秒)后调用它(您必须进行实验以查看看起来不错) 。例如
[NSTimer scheduledTimerWithTimeInterval:.2 target:self selector:@selector(presentButton) userInfo:nil repeats:NO];
这应该够了吧。