gpt4 book ai didi

ios - 如何正确覆盖 reloadRowsAtIndexPaths :withRowAnimation?

转载 作者:IT王子 更新时间:2023-10-29 05:43:56 25 4
gpt4 key购买 nike

我想在重新加载 UITableView 的行时创建一个特殊的动画。问题是我不想一直使用这个动画,因为我有时会使用内置动画来重新加载行。

那我该怎么做呢?通过在我自己的 UITableView 实现中覆盖 reloadRowsAtIndexPaths:withRowAnimation?怎么办?

或者也许有更好的方法在重新加载一行时获得我自己的动画?

最佳答案

我认为您不应该覆盖 reloadRowsAtIndexPaths:withRowAnimation。只需在 UITableView 的类别中实现一个自定义方法 reloadRowsWithMyAnimationAtIndexPaths: 并在需要时使用它。

但是如果你想在 UITableView 的子类中覆盖这个方法,你可以这样做:

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths 
withRowAnimation:(UITableViewRowAnimation)animation {
if (self.useMyAnimation)
[self reloadRowsWithMyAnimationAtIndexPaths:indexPaths];
else
[super reloadRowsAtIndexPaths:indexPaths withRowAnimation:animation];
}

self.useMyAnimation 只是一个标志(BOOL 属性),指示要使用的动画。在重新加载操作之前设置此标志。

对于 2 个或更多自定义动画,您可以实现枚举:

enum MyTableViewReloadAnimationType {
case None
case First
case Second
case Third
}

然后创建一个 MyTableViewReloadAnimationType 属性(例如,reloadAnimationType)并使用开关选择合适的动画方法:

    var reloadAnimationType = MyTableViewReloadAnimationType.None

override func reloadRowsAtIndexPaths(indexPaths: [AnyObject], withRowAnimation animation: UITableViewRowAnimation) {
switch self.reloadAnimationType {
case .None:
super .reloadRowsAtIndexPaths(indexPaths, withRowAnimation:animation)
default:
self .reloadRowsAtIndexPaths(indexPaths, withCustomAnimationType:self.reloadAnimationType)
}
}

func reloadRowsAtIndexPaths(indexPaths: [AnyObject], withCustomAnimationType animationType: MyTableViewReloadAnimationType) {
switch animationType {
case .First:
self .reloadRowsWithFirstAnimationAtIndexPaths(indexPaths)
case .Second:
self .reloadRowsWithSecondAnimationAtIndexPaths(indexPaths)
case .Third:
self .reloadRowsWithThirdAnimationAtIndexPaths(indexPaths)
}
}

您可以直接调用自定义方法reloadRowsAtIndexPaths:withCustomAnimationType::

self.tableView .reloadRowsAtIndexPaths([indexPath], withCustomAnimationType: MyTableViewReloadAnimationType.First)

在自定义方法中,您需要使用 dataSource 方法获取当前单元格和新单元格:

func reloadRowsWithFirstAnimationAtIndexPaths(indexPaths: [AnyObject]) {
for indexPath in indexPaths {
var currentCell = self .cellForRowAtIndexPath(indexPath as! NSIndexPath)
var newCell = self.dataSource .tableView(self, cellForRowAtIndexPath: indexPath as! NSIndexPath)
var newCellHeight = self.delegate .tableView(self, heightForRowAtIndexPath: indexPath)
var frame: CGRect = currentCell.frame
frame.size.height = newCellHeight
newCell.frame = frame
self .replaceCellWithFirstAnimation(currentCell!, withAnotherCell: newCell);
}
}

func replaceCellWithFirstAnimation(firstCell : UITableViewCell, withAnotherCell secondCell: UITableViewCell) {
var cellsSuperview = firstCell.superview!
//make this with animation
firstCell .removeFromSuperview()
cellsSuperview .addSubview(secondCell)
}

您需要处理 newCell 的高度 > 或 < 然后是 currentCell 的高度的情况。必须重新计算所有其他单元格框架。我认为可以使用 beginUpdates 和 endUpdates 方法来完成。在对细胞进行操作之前调用它们。

关于ios - 如何正确覆盖 reloadRowsAtIndexPaths :withRowAnimation?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29668592/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com