ios - 如何填充在其行中嵌入了 collectionViews 的 tableView 并按类型或类别组织它们?
问题描述
我有一个事件/场地管理应用程序和一个视图接收 json 格式的事件数据,其中每个事件都有一个类别或类型。如何填充和组织嵌入在 TableView 单元格中的 collectionViews 中的事件,其中每个 TableView 单元格都是要显示的类别或类型。如果可能的话,用 Swift 回答
解决方案
创建具有所有可用属性的结构并为类别添加枚举
struct Event {
enum Category {
case a
case b
case c
}
var category: Category
var eventName: String
//...
}
创建 Event 对象的二维数组
var groupedEventsArr = [[Event]]()
从服务器接收到数据后,创建一个 Event 对象数组。
let eventsArr:[Event] = //json parse
然后根据数组对象分组category
groupedEventsArr = Array(Dictionary(grouping: eventsArr, by: { $0.category }).values)
现在在tableview中使用这个数组,collection view数据源方法
//表格视图
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return groupedEventsArr.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
let category = groupedEventsArr[indexPath.row].first?.category
return cell!
}
//集合视图
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return groupedEventsArr[section].count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
let event = groupedEventsArr[indexPath.section][indexPath.item]
return cell
}
推荐阅读
- c# - 列表视图 MVVM 中的 selectedItem 时无法更改 Textblock 中的数据绑定
- wordpress - 使用 nginx 安装 codeigniter 和 wordpress 但博客内部 URL 不起作用
- c# - ASPNet Core:使用 [Authorize] with function in service
- corda - 在 R3 Corda 中我不知道启动 Web 服务器端口?
- groovy - 全局变量 - Katalon Studio
- python - 如何从张量流中的检查点恢复复杂对象,以通过恢复检查点预测新数据
- spring - liquibase 没有为 Spring Boot 更新数据库中的新数据
- mysql - 想在 ssis 中连接 oledb 和 mysql
- rest - 从表单中获取信息以调用 api
- angular - 如何隐藏角度 4/6 剑道网格数字过滤器增加减少图标?(纺纱机)