如何解决如何使用 CollectionView 组合布局实现多个部分和单元格类型?
我有一个类似于这个的数据模型
class TVScheduleController {
struct ShowsCollection: Hashable {
let title: String
let categories: [String]
let identifier = UUID()
func hash(into hasher: inout Hasher) {
hasher.combine(identifier)
}
}
struct LiveShow: Hashable {
let name: String
let identifier = UUID()
func hash(into hasher: inout Hasher) {
hasher.combine(identifier)
}
}
struct LiveShowCollection: Hashable {
let title: String
let shows: [LiveShow]
let identifier = UUID()
func hash(into hasher: inout Hasher) {
hasher.combine(identifier)
}
}
struct UpcomingShow: Hashable {
let name: String
let startsInHours: Int
let identifier = UUID()
func hash(into hasher: inout Hasher) {
hasher.combine(identifier)
}
}
struct UpcomingShowCollection: Hashable {
let title: String
let shows: [UpcomingShow]
let identifier = UUID()
func hash(into hasher: inout Hasher) {
hasher.combine(identifier)
}
}
var collections: [AnyHashable] {
return _collections
}
init() {
generateCollections()
}
fileprivate var _collections = [AnyHashable]()
}
我生成这样的集合
extension TVScheduleController {
func generateCollections() {
_collections = [
ShowsCollection(title: "TV Shows",categories: ["basketball","reality tv","news","soccer"]),LiveShowCollection(title: "Live Shows",shows: [LiveShow(name: "The Bachelor"),LiveShow(name: "CSPAN")]),UpcomingShowCollection(title: "Upcoming Today",shows: [UpcomingShow(name: "The Bachelor",startsInHours: 4),UpcomingShow(name: "CSPAN",startsInHours: 5)]),UpcomingShowCollection(title: "Upcoming Tomorrow",startsInHours: 24+4),startsInHours: 24+5)]),]
}
}
我希望每个集合 ShowsCollection
、LiveShowCollection
、UpcomingShowCollection
具有不同的布局。例如,“篮球”、“真人秀”、“新闻”、“足球”将显示为列表。直播节目将显示为 2 列网格,即将上映的节目将显示为 3 列网格。
为了指定布局,func createLayout() -> UICollectionViewLayout{...}
中的条件会是什么样子?
如何设置 UICollectionViewDiffableDataSource
以通过 collectionView.dequeueConfiguredReusableCell
返回正确的单元格?
最后,迭代所有部分并将它们及其项目附加到快照的正确方法是什么?
在设计首选数据模型方面是否还有其他不同的方法?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。