gpt4 book ai didi

ios - 在部分之间移动时 MoveRow 方法没有动画

转载 作者:行者123 更新时间:2023-11-29 05:33:31 25 4
gpt4 key购买 nike

我正在创建购物 list 应用程序。我使用 2 个部分来分隔已购买的元素和未购买的元素。我使用 moveRow 方法在所述两个部分之间移动行。这是移动行的代码。

if indexPath.section == 0 {
self.shoppingItems.remove(at: indexPath.row)
self.shoppingItemsBought.append(item)
self.tableView.beginUpdates()

let fromIndexPath = NSIndexPath(row: indexPath.row, section: 0)
let toIndexPath = NSIndexPath(row: 0, section: 1)

self.tableView.moveRow(at: fromIndexPath as IndexPath, to: toIndexPath as IndexPath)
self.tableView.endUpdates()

} else {
self.shoppingItemsBought.remove(at: indexPath.row)
self.shoppingItems.append(item)
self.tableView.beginUpdates()

let fromIndexPath = NSIndexPath(row: indexPath.row, section: 1)
let toIndexPath = NSIndexPath(row: 0, section: 0)

self.tableView.moveRow(at: fromIndexPath as IndexPath, to: toIndexPath as IndexPath)
self.tableView.endUpdates()
}

行更改其位置,但没有动画。我错过了什么?

最佳答案

一种选择是在 moveRow 周围使用 CATransaction。将您的更改放入 CATransaction block 中,完成后强制重新加载。这将通过动画重新加载。

示例:

CATransaction.begin()
CATransaction.setCompletionBlock {
tableView.reloadData()
}

tableView.beginUpdates()

// Update the datasource
if indexPath.section == 0 {
self.shoppingItems.remove(at: indexPath.row)
self.shoppingItemsBought.append(item)
}
else {
self.shoppingItemsBought.remove(at: indexPath.row)
self.shoppingItems.append(item)
}

// Get new IndexPath (indexPath is the old one, as in current)
let newIndexPath = IndexPath(row: 0, section: indexPath.section == 0 ? 1 : 0)

// Move the row in tableview
tableView.moveRow(at: indexPath, to: newIndexPath)

tableView.endUpdates()
CATransaction.commit()

关于ios - 在部分之间移动时 MoveRow 方法没有动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57295652/

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