ios 带动画的 UITableview 重新加载数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14576921/
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
UITableview reloaddata with animation
提问by Naveen Pathiyil
I have a UITableView
which is populated from array and a drop down list.
If I select any row of drop down list the array will be inserted with new values and tableview should get reloaded.
我有一个UITableView
从数组和下拉列表填充的。
如果我选择下拉列表的任何一行,数组将插入新值,并且应该重新加载 tableview。
How to animate the tableview with new array contents?
如何使用新的数组内容为 tableview 设置动画?
Animation like I want to show the row one by one. i tried this method
像我想一一显示行的动画。我试过这个方法
- (void)reloadRowsAtIndexPaths:(NSArray )indexPaths withRowAnimation:(UITableViewRowAnimation)animation {
NSIndexPath rowToReload = [NSIndexPath indexPathForRow:[names count] inSection:0];
NSArray* rowsToReload = [NSArray arrayWithObjects:rowToReload, nil];
[tableDetails reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationNone];
}
回答by Tim Windsor Brown
To add to the correct answer, if you want to reload allsections in your UITableView
you will need to do the following:
要添加到正确答案中,如果您想重新加载您的所有部分,您UITableView
需要执行以下操作:
ObjC
对象
NSRange range = NSMakeRange(0, [self numberOfSectionsInTableView:self.tableView]);
NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange:range];
[self.tableView reloadSections:sections withRowAnimation:UITableViewRowAnimationAutomatic];
Swift
迅速
let range = NSMakeRange(0, self.tableView.numberOfSections)
let sections = NSIndexSet(indexesIn: range)
self.tableView.reloadSections(sections as IndexSet, with: .automatic)
This will reload everything in the table view with an animation. Like a boss.
这将使用动画重新加载表格视图中的所有内容。像一个老板一样。
回答by Tom Howard
Swift 5:
斯威夫特 5:
UIView.transition(with: tableView, duration: 1.0, options: .transitionCrossDissolve, animations: {self.tableView.reloadData()}, completion: nil)
Swift 3
斯威夫特 3
UIView.transition(with: myTableView, duration: 1.0, options: .transitionCrossDissolve, animations: {self.myTableView.reloadData()}, completion: nil)
Swift 2
斯威夫特 2
UIView.transitionWithView(myTableView, duration: 1.0, options: .TransitionCrossDissolve, animations: {self.myTableView.reloadData()}, completion: nil)
I went with this in Swift
我在 Swift 中使用了这个
回答by macpandit
use this method,
用这个方法,
[_tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
回答by Jai Govindani
You first tell the tableView to expect updates with beginUpdates
. Then update the relevant sections (if your tableView has only one section then just pass zero for the section number). Here's where you specify the animation - you can play around with it to get the effect that you want. After that you call endUpdates
on the tableView. The typedef for UITableViewRowAnimation specifies:
您首先告诉 tableView 期待更新beginUpdates
。然后更新相关部分(如果您的 tableView 只有一个部分,则只需为部分编号传递零)。您可以在此处指定动画 - 您可以使用它来获得所需的效果。之后,您调用endUpdates
tableView。UITableViewRowAnimation 的 typedef 指定:
typedef enum {
UITableViewRowAnimationFade,
UITableViewRowAnimationRight,
UITableViewRowAnimationLeft,
UITableViewRowAnimationTop,
UITableViewRowAnimationBottom,
UITableViewRowAnimationNone,
UITableViewRowAnimationMiddle,
UITableViewRowAnimationAutomatic = 100
} UITableViewRowAnimation;
Play around to see which one you want. Even selecting UITableViewRowAnimationNone
can have a nice effect sometimes. Table update code below:
四处看看,看看你想要哪一个。即使选择UITableViewRowAnimationNone
有时也会有很好的效果。表更新代码如下:
[self.tableView beginUpdates];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
回答by Natanel
In Swift 3 you can simply add the following extension
to UITableView
:
在 Swift 3 中,您可以简单地将以下内容添加extension
到UITableView
:
extension UITableView {
func reloadData(with animation: UITableView.RowAnimation) {
reloadSections(IndexSet(integersIn: 0..<numberOfSections), with: animation)
}
}
回答by arthankamal
you can use reloadSections:withRowAnimation:
functions. Check UITableView Class Reference.
你可以使用reloadSections:withRowAnimation:
函数。检查UITableView 类参考。
Check this reloadData with Animationwhich already answered your question.
检查这个reloadData with Animation已经回答了你的问题。
回答by Jayprakash Dubey
Below method is supported by tableView for animation :
tableView 支持以下方法用于动画:
- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0);
Usage :
用法 :
//Reload tableView with animation
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationRight];
Different types of animation available are :
可用的不同类型的动画是:
typedef NS_ENUM(NSInteger, UITableViewRowAnimation) {
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 };
Hope this might help!
希望这可能会有所帮助!
回答by Priyank Jotangiya
I have tried this and my Table
is Animated
with smooth animation
我曾尝试这样做,我Table
是Animated
用流畅的动画效果
//Put this code where you want to reload your table view
//将此代码放在要重新加载表视图的位置
dispatch_async(dispatch_get_main_queue(), ^{
[UIView transitionWithView:<"TableName">
duration:0.1f
options:UIViewAnimationOptionTransitionFlipFromRight
animations:^(void) {
[<"TableName"> reloadData];
} completion:NULL];
});
回答by Mithun Ravindran
Try Animating while Transition Change of a view while reloading UITableView
在重新加载 UITableView 时尝试在转换视图时设置动画
[UIView transitionWithView:_yourTableView
duration:0.40f
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^(void) {[_yourTableView reloadData];}
completion:nil];
回答by Erumaru
Swift 5:
斯威夫特 5:
let section = 0
tableView.reloadSections([section], with: .automatic)