【问题标题】:Add/Delete UITableViewCell with animation?添加/删除带有动画的 UITableViewCell?
【发布时间】:2011-04-06 01:40:52
【问题描述】:

我知道这听起来可能是个愚蠢的问题,但我到处都看过。我该怎么做?

我知道如何使用 swype-to-delete 方法执行此操作,但我如何在该功能之外执行此操作?

请发布一些代码示例。

谢谢!
库尔顿

【问题讨论】:

    标签: objective-c ios4 uitableview


    【解决方案1】:
    [self.tableView beginUpdates];
    [self.tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationFade];
    [self.tableView deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade];
    [self.tableView endUpdates];
    

    insertIndexPaths 是要插入到表中的 NSIndexPaths 数组。

    deleteIndexPaths 是要从表中删除的 NSIndexPaths 数组。

    索引路径的示例数组格式:

    NSArray *insertIndexPaths = [[NSArray alloc] initWithObjects:
            [NSIndexPath indexPathForRow:0 inSection:0],
            [NSIndexPath indexPathForRow:1 inSection:0],
            [NSIndexPath indexPathForRow:2 inSection:0],
            nil];
    

    【讨论】:

    • 感谢您的回答!我这样使用你的方法:` NSArray *insertIndexPaths = [[NSArray alloc] initWithObjects:[NSIndexPath indexPathForRow:0 inSection:0], nil]; [表格视图开始更新]; [tableView deleteRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationLeft]; [theTableView endUpdates];` 但它仍然什么也没做。我认为这与theTableView 部分有关。尝试self.tableView 时,它告诉我它不是结构或联合...谢谢!
    • 你的视图控制器需要继承 UITableViewController,或者如果它只是一个 UIViewController 它需要实现 UITableViewDelegate 和 UITableViewDataSource 协议。然后在界面生成器中,将表视图添加到您的视图并将其委托和数据源属性链接到您的视图控制器。
    • 我的子类为 UIViewController,带有 UITableViewDelegate 和 DataSource(在我的视图中我将它们链接到我的 UITableView)。我还能做错什么?
    • 对于需要保留其 UIViewController 的任何人,只需制作一个 IBOutlet,在 .h 中添加一个属性,然后在您的 .m 中合成它。然后调用self.yourTableViewOutlet 就可以了。
    • 收到此错误.. Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (4) must be equal to the number of rows contained in that section before the update (4), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out)
    【解决方案2】:

    在 Swift 2.1+ 中

    tableView.beginUpdates()
    tableView.deleteRowsAtIndexPaths([YourIndexPathYouWantToDeleteFrom], withRowAnimation: .Fade)
    tableView.insertRowsAtIndexPaths([YourIndexPathYouWantToInsertTo], withRowAnimation: .Fade)
    tableView.endUpdates()
    

    你可以像这样轻松地创建一个 NSIndexPath:

    NSIndexPath(forRow: 0, inSection: 0)
    

    【讨论】:

      【解决方案3】:

      你想要这两种方法:insertRowsAtIndexPaths:withRowAnimation:和deleteSections:withRowAnimation:它们都在UITableView documentation中有详细说明。

      【讨论】:

      • 感谢您的回复。这就是我正在做的事情:` [processList removeObjectAtIndex:0]; [表格视图开始更新]; [tableView deleteRowsAtIndexPaths:0 withRowAnimation:UITableViewRowAnimationFade]; [tableView deleteSections:0 withRowAnimation:UITableViewRowAnimationFade]; [theTableView endUpdates];` 但是什么也没发生。
      【解决方案4】:

      斯威夫特 4.0

          tableView.beginUpdates()
          tableView.deleteRows(at: [YourIndexPathYouWantToDeleteFrom], with: .fade)
          tableView.insertRows(at: [YourIndexPathYouWantToInsertTo], with: .fade)
          tableView.endUpdates()
      

      要创建 IndexPath,请使用:

      IndexPath(row: 0, section: 0)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-07-28
        • 2018-10-31
        • 1970-01-01
        • 1970-01-01
        • 2011-10-21
        • 1970-01-01
        • 1970-01-01
        • 2014-02-16
        相关资源
        最近更新 更多