用动画重新加载数据

我有一个从数组和下拉列表填充的 UITableView
如果我选择任何一行下拉列表,数组将被插入新值,tableview 应该被重新加载。

如何使用新的数组内容动画表视图?

动画像我想显示一行一个。我尝试了这个方法

- (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];
}
93447 次浏览

您可以使用 reloadSections:withRowAnimation:函数。请检查 UITableView 类引用

检查这个 带动画的 reloadData已经回答了你的问题。

使用这种方法,

[_tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];

首先告诉 tableView 使用 beginUpdates进行更新。然后更新相关的部分(如果 tableView 只有一个部分,那么只需要为部分编号传递零)。这里是您指定动画的地方-您可以使用它来获得您想要的效果。然后在 tableView 上调用 endUpdates。UITableViewRowAnimation 的 typedef 指定:

typedef enum {
UITableViewRowAnimationFade,
UITableViewRowAnimationRight,
UITableViewRowAnimationLeft,
UITableViewRowAnimationTop,
UITableViewRowAnimationBottom,
UITableViewRowAnimationNone,
UITableViewRowAnimationMiddle,
UITableViewRowAnimationAutomatic = 100
} UITableViewRowAnimation;

看看你想要哪一个。即使选择 UITableViewRowAnimationNone有时也会有很好的效果。表更新代码如下:

    [self.tableView beginUpdates];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];

TableView 支持下面的方法用于动画:

- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0);

用法:

//Reload tableView with animation
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationRight];

不同类型的动画有:

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

希望这个能帮上忙!

为了补充正确答案,如果你想在你的 UITableView中重新加载 所有部分,你需要做以下事情:

目的

NSRange range = NSMakeRange(0, [self numberOfSectionsInTableView:self.tableView]);
NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange:range];
[self.tableView reloadSections:sections withRowAnimation:UITableViewRowAnimationAutomatic];

斯威夫特

let range = NSMakeRange(0, self.tableView.numberOfSections)
let sections = NSIndexSet(indexesIn: range)
self.tableView.reloadSections(sections as IndexSet, with: .automatic)

这将用动画重新载入表格视图中的所有内容,就像老板一样。

在重新加载 UITableView 时尝试动画转换视图

[UIView transitionWithView:_yourTableView
duration:0.40f
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^(void) {[_yourTableView reloadData];}
completion:nil];

我已经尝试了这一点,我的 TableAnimated顺利动画

//将此代码放在要重新加载表视图的位置

dispatch_async(dispatch_get_main_queue(), ^{
[UIView transitionWithView:<"TableName">
duration:0.1f
options:UIViewAnimationOptionTransitionFlipFromRight
animations:^(void) {
[<"TableName"> reloadData];
} completion:NULL];
});

迅捷5:

UIView.transition(with: tableView, duration: 1.0, options: .transitionCrossDissolve, animations: {self.tableView.reloadData()}, completion: nil)

Swift 3

UIView.transition(with: myTableView, duration: 1.0, options: .transitionCrossDissolve, animations: {self.myTableView.reloadData()}, completion: nil)

Swift 2

UIView.transitionWithView(myTableView, duration: 1.0, options: .TransitionCrossDissolve, animations: {self.myTableView.reloadData()}, completion: nil)

我在斯威夫特就是这么做的

在 Swift 3中,你可以简单地将以下 extension添加到 UITableView:

extension UITableView {
func reloadData(with animation: UITableView.RowAnimation) {
reloadSections(IndexSet(integersIn: 0..<numberOfSections), with: animation)
}
}

迅捷5:

let section = 0
tableView.reloadSections([section], with: .automatic)

将 dislplaycell 粘贴此代码,它将动画添加新的单元格到 tableview

let rotationTransform = CATransform3DTranslate(CATransform3DIdentity, 0, 50, 0)
cell.layer.transform = rotationTransform
cell.alpha = 0.0
UIView.animate(withDuration: 0.05 * indexPath.row) {
cell.layer.transform = CATransform3DIdentity
cell.alpha = 1.0
}