UITableViewDiffableDataSource 设置不同的单元格动画

问题描述 投票:0回答:1

我正在使用

UITableViewDiffableDataSource
来渲染单元格,我想知道如何精细地对行进行动画处理。

有房产

self.dataSource?.defaultRowAnimation = .fade

适用于 tableView 的所有行,但我想要某些单元格有一些动画,而其他单元格没有动画。 “defaultRowAnimation”中的“default”表明还有其他方法。

不幸的是,Apple 关于该主题的文档有点简单

有没有一种方法可以根据给定的

RowAnimation
询问
IndexPath
例如?

swift uitableview diffabledatasource
1个回答
0
投票

不幸的是,没有委托方法或类似方法询问您为哪个单元格使用哪个动画,但是在

UITableViewDiffableDataSource.update(sections:)
中,您可以根据您喜欢的任何标准选择要使用的动画,只要它在模型中表示即可。例如


class MyTableViewDataSource: UITableViewDiffableDataSource<ListSectionID, ListItemID> {

func update(animated: Bool = true) {
    var newSnapshot = NSDiffableDataSourceSnapshot<ListSectionID, ListItemID>()
    newSnapshot.appendSections([.theSection])

    if let items = DataManager.shared.itemsUsingFadeAnimation {
        let ids = items.map { ListItemID(id: $0.id) }
        newSnapshot.appendItems(items, toSection: .theSection)
        .defaultRowAnimation = .fade
        apply(newSnapshot, animatingDifferences: animated)
    }
    if let items = DataManager.shared.itemsUsingMiddleAnimation {
        let ids = items.map { ListItemID(id: $0.id) }
        // insert these ids into the snapshot using `appendItems` or `insertItems`
        .defaultRowAnimation = .middle
        apply(newSnapshot, animatingDifferences: animated)
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.