相关疑难解决方法(0)

仅考虑 id 来符合 Hashable 是否正确?

我遇到了很多在线示例,当他们尝试符合时Hashable,他们只考虑id考虑。例如https://www.raywenderlich.com/8241072-ios-tutorial-collection-view-and-diffable-data-source , https://medium.com/@JoyceMatos/hashable-protocols-in-swift-baf0cabeaebd , ...

/// Copyright (c) 2020 Razeware LLC
/// 
/// Permission is hereby granted, free of charge, to any person obtaining a copy
/// of this software and associated documentation files (the "Software"), to deal
/// in the Software without restriction, including without limitation the rights
/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
/// copies of the Software, and to permit persons to whom the Software is
/// …
Run Code Online (Sandbox Code Playgroud)

hashable swift

9
推荐指数
2
解决办法
758
查看次数

如何使用 diffable UITableView 更新表格单元格

我在 UITableView 中使用了新的 NSDiffableDataSourceSnapshot 和 UITableViewDiffableDataSource。我在构建表格时没有问题,但是当单元格中显示的数据发生变化时,我在更新单元格时遇到问题。我还没有找到任何解释如何执行此操作的 Apple 文档。我尝试了以下方法:

self.currentSnapshot.reloadItems([Item(identifier: identifier)])
self.dataSource.apply(self.currentSnapshot)
Run Code Online (Sandbox Code Playgroud)

我在 reloadItems 中收到以下错误:

断言失败 -[__UIDiffableDataSourceSnapshot _reloadViewUpdatesForDiffUpdate:dataSource:ignoreInvalidItems:]

我已经检查过传递给 Item 初始值设定项的标识符是否已经存在于快照中。

这是我的项目类:

class Item: Hashable, Equatable {

    let identifier: String
    var matchWrapper: MatchWrapper

    init(matchWrapper: MatchWrapper) {
        self.identifier = matchWrapper.identifier
        self.matchWrapper = matchWrapper
    }

    func hash(into hasher: inout Hasher) {
        hasher.combine(self.identifier)
    }

    static func == (lhs: ScoresViewController.Item, rhs: ScoresViewController.Item) -> Bool {
        return lhs.identifier == rhs.identifier
    }
}
Run Code Online (Sandbox Code Playgroud)

有什么建议?

uitableview

8
推荐指数
1
解决办法
1062
查看次数

标签 统计

hashable ×1

swift ×1

uitableview ×1