在UITableVIew中animation插入新的部分

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

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

而且在我的-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 。 我的部分被添加,因为它应该,但我已经读过的地方,这可以animation,因为当我按下我的button部分添加,但没有animation。 我想我应该使用这个-(void)insertSections:(NSIndexSet*)sections withRowanimation(UITableViewRowAnimation) animation但我还没有find一个适当的例子在networking上。

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; 

基本上它告诉表格视图从哪个方向你想要的行/部分animation进/出。 稍微的试验将会certificate每个的效果。

例如,使用UITableViewRowAnimationTop插入一行可以触发一个animation,这个animation给人一种从表视图中的最终目的地之上的空间进入表视图的行的印象。

所以你的插入可能看起来像:

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

你必须确保你的委托和数据源提供的信息与你的断言插入部分/行一致。 从你的问题,看起来你已经这样做了。

EDITS:

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

清除泥浆?

更新

如果你正在编写显式animation,那么我可以说你可以在“完成处理程序”中完成(或者直接为此编写animation),但是在这里你不可用。 我认为你可以在视图中将button显示代码包装在自己的方法中,然后设置一个计时器,在很短的时间内调用它,例如0.2秒(你将不得不尝试看看效果如何) 。 例如

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

这应该够了吧。

使用以下API(使用最后两个API),使用animation将行和部分操作成UITableView变得非常简单:

  • insertRowsAtIndexPaths:withRowAnimation:
  • insertSections:withRowAnimation:
  • deleteRowsAtIndexPaths:withRowAnimation:
  • deleteSections:withRowAnimation:
  • beginUpdates
  • endUpdates

更新数据模型

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

不要为新插入的行插入行正在插入

使用insertSections:withRowAnimation:方法添加新节时,不需要调用insertRowsAtIndexPaths:withRowAnimation:向其中添加行。 insertRowsAtIndexPaths:withRowAnimation:方法仅用于animation现有节更改。 同样,您不需要调用deleteRowsAtIndexPaths:withRowAnimation:当您使用deleteSections:withRowAnimation:删除节时。

关于删除和插入:

我通常在独立的beginUpdates: endUpdates调用中执行这些操作,但是您可以同时插入和删除。 只要知道,UITableView将首先尝试删除行/节, 然后尝试插入它们, 无论您在代码中执行的顺序如何

讨论部分的deleteRowsAtIndexPaths:withRowAnimation:

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

插入节的示例:

 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]; 
 - (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation 

这个指出的方法肯定会做到这一点。

也许你可以试试这个:

 [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade]; 

直接写出我的想法..希望这可以帮助…

请注意:

  1. 任何的UITableViewRowAnimation都可以使用。
  2. 所提到的代码中的sectionIndex是一个NSInteger。 可能在你的情况1。