UITableView添加单元格动画

问题描述:

任何人都可以帮我解决UITableView动画问题吗?UITableView添加单元格动画

默认情况下,我们有用于删除单元格和重新排序UITableView中的单元格的动画。

我们可以添加动画添加单元格,如果是的话,该怎么做。

我已签出Three20,没没有得到如何twitter做了桌子底下我的资料>锐推扩大动画。

想要使用UITableView中的现有动画在没有Three20 frameowrk,的情况下尝试。

可以使用以下方法的UITableView:

- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation 

实施例与self.dataSource是一个可变的阵列和你的表仅具有1个部分:

[self.dataSource addObject:@"New Item"]; 
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:[self.dataSource count]-1 inSection:0]; 
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 

注意:数据源从计数中减去1因为NSIndexPath是零索引。

+0

感谢您的代码,你能给我一个工作样本吗? o 我对于如何在插入时更新数据源稍有困惑。 – Ameya 2010-06-26 08:07:45

+0

早些时候我会简单地添加一个记录到数据源(NSMutableArray)并调用[self.tableView reloadData];但这并不会带来新的元素。 – Ameya 2010-06-26 08:11:40

+1

只是一个小的变化,我们的代码@JK PLZ编辑你的答案,希望它有助于一些寻找这种解决方案。 [tableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:[self。dataSource count] inSection:0],nil] withRowAnimation:UITableViewRowAnimationRight]; – Ameya 2010-06-26 10:36:31

夫特

例数组,它是用于表视图

var myArray = ["Cow", "Camel", "Sheep", "Goat"] 

以下将在表视图的顶部添加有动画的行的数据源。

// add item to current array 
myArray.insert("Horse", atIndex: 0) 

// insert row in table 
let indexPath = NSIndexPath(forRow: 0, inSection: 0) 
tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade) 

多次更新

如果您需要做多次插入和/或删除,然后用beginUpdates()endUpdates()包围他们。

tableView.beginUpdates() 
tableView.insertRowsAtIndexPaths([addIndexPath1, addIndexPath2, ...], withRowAnimation: .Fade) 
tableView.deleteRowsAtIndexPaths([deleteIndexPath1, deleteIndexPath2, ...], withRowAnimation: .Fade) 
tableView.endUpdates() 

进一步阅读

+1

我正在使用您的代码来插入我需要插入的行,不过无论我选择哪种动画类型,它总是以相同的方式进行动画处理(即淡入淡出,左侧,所有都具有相同的行为) 。任何想法为什么发生这种情况 – 2017-05-09 13:56:05

+0

@GeorgeAvgoustis,我还没有为此工作过一段时间,所以我不知道。我会把它列在我要检查的事情清单上,但我可能暂时没有办法。如果你先弄明白,请留下另一条评论。 – Suragch 2017-05-09 14:27:15

使用reloadRowsAtIndexPaths:indexPath

tableView.beginUpdates() 
    [tableView reloadRowsAtIndexPaths:indexPath withRowAnimation:UITableViewRowAnimationAutomatic]; 
    tableView.endUpdates() 

我希望这将帮助你..