我有一个UITableView
,它是从数组和一个下拉列表中填充的。如果我选择下拉列表的任何行,则将使用新值插入数组,并且应该重新加载tableview。
如何使用新的数组内容为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];
}
要添加正确的答案,如果您想在UITableView
中重新加载all部分,则需要执行以下操作:
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)
这将用动画重新加载表格视图中的所有内容。像老板一样。
使用此方法,
[_tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
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中是这样的
您首先告诉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];
在Swift 3中,您可以简单地将以下extension
添加到UITableView
:
extension UITableView {
func reloadData(with animation: UITableView.RowAnimation) {
reloadSections(IndexSet(integersIn: 0..<numberOfSections), with: animation)
}
}
您可以使用reloadSections:withRowAnimation:
功能。检查UITableView Class Reference。
选中此reloadData with Animation,它已经回答了您的问题。
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 };
希望这会有所帮助!
我已经尝试过,并且我的Table
是Animated
且动画流畅)>
///将此代码放在要重新加载表格视图的位置
dispatch_async(dispatch_get_main_queue(), ^{
[UIView transitionWithView:<"TableName">
duration:0.1f
options:UIViewAnimationOptionTransitionFlipFromRight
animations:^(void) {
[<"TableName"> reloadData];
} completion:NULL];
});
尝试在过渡时进行动画处理,并在重新加载UITableView时更改视图