【问题标题】:UITableView add cell AnimationUITableView 添加单元格动画
【发布时间】:2010-06-26 06:18:57
【问题描述】:

谁能帮我解决UITableView 动画问题?

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

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

我已经查看了 Three20,不明白 twitter 如何在 MyProfile>ReTweets 下完成表格展开动画。

想在不使用Three20 frameowrk, 的情况下使用UITableView 中的现有动画进行尝试。

【问题讨论】:

    标签: ios iphone uitableview cocoa-touch animation


    【解决方案1】:

    您可以使用以下 UITableView 方法:

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

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

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

    【讨论】:

    • 谢谢你的代码,你能给我一个工作样本吗? o 我对如何在插入时更新数据源感到有些困惑。
    • 之前我只是简单地向数据源 (NSMutableArray) 添加一条记录并调用 [self.tableView reloadData];但这不会生动地引入新元素。
    • 只是您的代码@JK plz 中的一个小改动,请编辑您的答案,希望它对寻找这种解决方案的人有所帮助。 [tableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:[self.dataSource count] inSection:0],nil] withRowAnimation:UITableViewRowAnimationRight];
    • 不要伪造[self.tableView beginUpdates][self.tableView endUpdates]包围
    • 行从0开始枚举,所以new index = self.dataSource.count会导致异常,应该是NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:self.dataSource.count -1 inSection:0];。 @Run Loop 请编辑你的答案
    【解决方案2】:

    斯威夫特

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

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

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

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

    多次更新

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

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

    进一步阅读

    【讨论】:

    • 我正在使用您的代码插入我需要插入的行但是,无论我选择哪种动画类型,它总是以相同的方式进行动画处理(即淡入淡出、底部、左侧,都具有相同的行为) .任何想法为什么会发生这种情况?
    • @GeorgeAvgoustis,我有一段时间没做这个了,所以我不知道。我会把它放在我要检查的事情清单上,但我可能有一段时间不会去做。如果您先弄清楚,请发表其他评论。
    【解决方案3】:

    使用 reloadRowsAtIndexPaths:indexPath

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

    我希望这会对你有所帮助..

    【讨论】:

      【解决方案4】:

      在 swift 中也可以使用:

      func insertRow(entries: [String]) {
          tableView.performBatchUpdates({
              self.tableView.insertRows(at: [IndexPath(row: entries.count - 1, section: 0)], with: .bottom)
          }, completion: nil)
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-04-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-20
        • 1970-01-01
        • 1970-01-01
        • 2011-10-17
        相关资源
        最近更新 更多