我有一个简单的表视图,其中单元格有一个数字标签,并且它们根据其行号进行编号。这一切工作正常,直到我试图插入一个新的行顶部。插入新行时的更新表视图编号
我需要一种方法来更新下面的所有单元格,以便编号是正确的。重新加载tableview
的作品,但它切断了行插入动画。任何人都知道这个解决方案?
谢谢!
我有一个简单的表视图,其中单元格有一个数字标签,并且它们根据其行号进行编号。这一切工作正常,直到我试图插入一个新的行顶部。插入新行时的更新表视图编号
我需要一种方法来更新下面的所有单元格,以便编号是正确的。重新加载tableview
的作品,但它切断了行插入动画。任何人都知道这个解决方案?
谢谢!
,如果你设置了正确的cellForRowAt指数函数,其中每个标签数量从indexpath.row然后导出之后再插入发生就叫
tableView.reloadData()
正如我已经提到的那样,它会切断动画。 –
哎呀抱歉,没有看到。是的,我之前也遇到过这个问题,您可以尝试使用reloadSections方法,但我认为我从来没有碰运气。所以相反,我认为我最终设置了一个计时器,在插入动画(可能是0.3秒)后关闭,然后重新加载表视图。不漂亮,但它的工作 – MobileMon
您可能需要使用该解决方案(雨燕3.0 ):
CATransaction.begin()
tableView.beginUpdates()
CATransaction.setCompletionBlock {() -> Void in
// this is supposed to be done after insertion animation
self.tableView.reloadSections(IndexSet([0]), with: UITableViewRowAnimation.automatic)
}
self.tableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: UITableViewRowAnimation.automatic)
tableView.endUpdates()
CATransaction.commit()
刷新你的表行插入完成后,如工作围绕下面的代码可以帮助:
[CATransaction begin];
[tableView beginUpdates];
[CATransaction setCompletionBlock: ^{
//Reload your tableView here
}];
[tableView insertRowsAtIndexPaths: indexPaths
withRowAnimation: UITableViewRowAnimationAutomatic];
[tableView endUpdates];
[CATransaction commit];
使用'indexPathsForVisibleRows'来检索可见的行,然后可以遍历返回的数组,使用'cellForRowAtIndexPath'来检索相应的单元格并更新其标签中的行号。你也可以尝试'tableView.reloadRowsAtIndexPaths(tableView.indexPathsForVisibleRows(),withRowAnimation:.None)',但这也可能会中断动画 – Paulw11