【问题标题】:UITableview reloaddata with animation带有动画的 UITableview reloaddata
【发布时间】:2013-01-12 16:05:05
【问题描述】:

我有一个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];
 }

【问题讨论】:

  • 什么样的动画?你有什么尝试做到这一点?
  • 动画就像我想一一显示行。我试过这个方法 - (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]; }
  • 将此代码放入您的问题中,以便有人可以更好地帮助您。

标签: ios uitableview reloaddata


【解决方案1】:

要添加到正确答案,如果您想重新加载UITableView 中的所有部分,您需要执行以下操作:

ObjC

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) 

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

【讨论】:

  • 这假设节数没有改变。我用这个测试保护了它[self.tableView numberOfSections] == [self numberOfSectionsInTableView:self.tableView] 否则我只打电话给[self.tableView reloadData]
  • 是的,这是一种彻底的处理方式。
  • @AxelGuilmin 你是对的,否则它会崩溃!
  • Swift 3 - let range = NSMakeRange(0, self.tableView.numberOfSections) let section = NSIndexSet(indexesIn: range) self.tableView.reloadSections(sections as IndexSet, with: .automatic)跨度>
  • @TimWindsorBrown 如何用新数据“与原始数组不同的数据”重新加载表视图
【解决方案2】:

斯威夫特 5:

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

斯威夫特 3

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

斯威夫特 2

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

我在 Swift 中使用了这个

【讨论】:

  • 这是最好的解决方案,因为它允许在调用前后重新加载具有不同行数的部分
【解决方案3】:

使用这个方法,

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

【讨论】:

  • 如果您有多个部分或在重新加载期间部分数量发生变化,则它无法正常工作
  • @AlexeyF 然后你会将它们全部插入到该 NSIndexSet 中,然后调用上面的方法。如果做不到,说明你的代码设计有问题。
  • 如果你想保留部分标题而不是行,它也不会产生正确的效果..
【解决方案4】:

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

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

【讨论】:

  • 这对我的问题来说是一个很棒的解决方案。谢谢老兄!
  • 很好的解决方案。对于使用 swift 4 的用户,UITableViewRowAnimation 已重命名为 UITableView.RowAnimation。
【解决方案5】:

你首先告诉 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];

【讨论】:

    【解决方案6】:

    您可以使用reloadSections:withRowAnimation: 函数。检查UITableView Class Reference。

    检查这个reloadData with Animation,它已经回答了你的问题。

    【讨论】:

    • 但是我必须把代码放在哪里? [tableDetails reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade] ?
    • 选择下拉菜单后,您可能会调用一个方法,将这段代码放在那里。
    • 下拉,因为它是 uitableview,只有我在 indexpath 调用 didselect 行,然后我将用新内容填充数组,然后我调用 [tableview relaoaddata]。
    • 然后在您的didselectrow中执行此操作
    【解决方案7】:

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

    希望这可能会有所帮助!

    【讨论】:

      【解决方案8】:

      我已经尝试过了,我的 Table 是 Animated,动画流畅

      //把这段代码放在你想重新加载表格视图的地方

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

      【讨论】:

      • 好主意,解决了很多问题(例如,需要避免在使用其他方法时删除最后一行时崩溃等)。将 jump-reloadData 变成平滑的 reloadData 的一种偷偷摸摸的方法。加 500!
      【解决方案9】:

      尝试在重新加载 UITableView 时更改视图的过渡动画

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

      【讨论】:

        【解决方案10】:

        斯威夫特 5:

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

        【讨论】:

          【解决方案11】:

          在将显示单元格粘贴此代码,它将以动画方式将新单元格添加到表格视图

          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
                 }
          

          【讨论】:

            猜你喜欢
            • 2011-11-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-01-06
            • 2011-08-02
            • 2019-06-12
            • 1970-01-01
            相关资源
            最近更新 更多