Ios 在UITableVIew中为插入新节设置动画
我的视图上有这个按钮,当我按下它时,我会在我的表视图中插入一个新的节(我的表视图中有一个逻辑条件)Ios 在UITableVIew中为插入新节设置动画,ios,uitableview,animation,Ios,Uitableview,Animation,我的视图上有这个按钮,当我按下它时,我会在我的表视图中插入一个新的节(我的表视图中有一个逻辑条件) -(NSInteger) numberOfSectionsInTableView:(UITableView*)tableView { if (slide==TRUE) return 2; return 1; } 在我的-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSInde
-(NSInteger) numberOfSectionsInTableView:(UITableView*)tableView
{
if (slide==TRUE) return 2;
return 1;
}
在我的
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
中,我的分区是按它应该的方式添加的,但我在某个地方读到过,这可以设置动画,因为当我按下按钮时,分区被添加,但没有动画。我想我应该使用这个-(void)insertSections:(NSIndexSet*)带有RowAnimation(UITableViewRowAnimation)动画的节
,但我在web上找不到合适的示例。UITableViewRowAnimation
是在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;
基本上,它告诉表视图,您希望从哪个方向将行/节设置为动画输入/输出。通过一点实验将演示每个行/节的效果
例如,插入带有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];
}
您必须确保您的委托和数据源提供的信息与插入节/行的断言一致
编辑:
我认为您不必调用reloadData
,UITableView
要求您的数据模型反映您使用insert/delete方法所做的更改。例如,如果在调用insertSections:withRowAnimation:
(使用它插入单个节),您的numberOfSectionsInTableView:
方法返回1,然后在调用之后,它必须返回2。否则将引发异常。正是这种一致性强制允许您(我想也是这样)为了避免调用reloadData
,需要的数据将由整个beginUpdates:
endUpdates:
事务重新加载,并且您在该事务期间对模型所做的任何更新都必须与插入/删除调用一一匹配
清澈如泥
更新
如果您正在编程显式动画,那么我会说您可以在“完成处理程序”中完成(或者直接为此编程动画),但您在此处不可用。我认为您可以在视图中用自己的方法包装显示代码的按钮,然后设置一个计时器,在短时间后调用它,例如,.2秒(您必须进行实验才能看到什么是好的)
这应该能奏效
- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation
他指出的这种方法肯定会起到神奇的作用
也许你可以试试这个:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
直接从我的脑海中写下…希望这有帮助
请注意:
UITableViewRowAnimation
sectionIndex
是一个NSInteger。可能在您的案例1中使用以下API(着重于最后两个API),使用动画将行和节操纵到UITableView中变得非常简单:
不幸的是,我现在不在我的Mac电脑上,所以我无法测试它,但这对我来说似乎很清楚,我坚信它会起作用。此外,如果我调用
insertSections
,我还需要调用reloadData
?是的,这已经很清楚了。在我测试之后,我会让你知道它是如何运行的。谢谢你的帮助,你的answer非常好。此代码缺少对UITableView
的beginUpdate
和endUpdates
方法的调用,因此无法工作。此外,在beginUpdate
和endUpdates
调用之间的某个位置更新数据模型也很重要。最后,如果调用,则没有必要进行动画reloadData
,因为这将重新加载所有数据,并根据新的表数据重新渲染任何可见的tableview单元格。因此,如果您想要动画,请不要调用reloadData
。Edited。感谢您对reloadData:
的信心,我不确定。效果很好,谢谢
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
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];