ios UITableView 添加单元格动画

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3122934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:23:19  来源:igfitidea点击:

UITableView add cell Animation

iosiphoneuitableviewcocoa-touchanimation

提问by Ameya

Can any one help me out with UITableViewanimating issue?

任何人都可以帮助我解决UITableView动画问题吗?

By default we have animation for deleting cell and reordering cells in UITableView.

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

Can we have animated adding cell, if so how to do it.

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

I have checked out Three20, did not not get how twitterhas done the table expand animation under MyProfile>ReTweets.

我查了Three20,没明白MyProfile>ReTweetstwitter下的表格展开动画是怎么做的。

Want to try it without Three20 frameowrk,using the existing animation in UITableView.

想在不Three20 frameowrk,使用现有动画的情况下尝试一下UITableView

回答by RunLoop

You can use the following UITableView method:

您可以使用以下 UITableView 方法:

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

Example with self.dataSource being a mutable array and your table only having 1 section:

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

Note: Subtracted 1 from datasource count since NSIndexPath is zero indexed.

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

回答by Suragch

Swift

迅速

Example array that is the data source for the table view

作为表视图数据源的示例数组

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

The following will add a row with animation at the top of the table view.

下面将在表格视图的顶部添加一行带有动画的动画。

// 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)

Multiple updates

多次更新

If you need to do multiple insertions and/or deletions then surround them with beginUpdates()and endUpdates().

如果您需要进行多次插入和/或删除,则用beginUpdates()和 将它们括起来endUpdates()

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

Further reading

进一步阅读

回答by Muruganandam Sathasivam

Use reloadRowsAtIndexPaths:indexPath

使用 reloadRowsAtIndexPaths:indexPath

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

i hope this will help you..

我希望这能帮到您..