【发布时间】:2021-05-27 18:30:13
【问题描述】:
我要实现的目标的概述我正在尝试制作一个通知表格视图,每个通知都按其创建日期分组,因此表格视图部分将是创建日期的数量,每个部分都包含在此创建的通知节标题中的日期。 我已经搜索了很多,但没有得到一个绝对的答案如何使用 RxDataSource 数组是动态加载的,通过 API 接收到的日期?
class T : UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return array.count
}
}
我所发现的只是将这些部分设置为静态
ViewModel.AllNotificationsObservable
.map({ [NotificationSectionViewModel(header: "Yet", items: $0.filter{$0.createAt.toDate()!.toString(format: "yyyy-MM-dd") == Date().toString(format: "yyyy-MM-dd") }),
NotificationSectionViewModel(header: "Yesterday", items: $0)
]
})
.bind(to: NotificationTableView.rx.items(dataSource: ViewModel.dataSource))
.disposed(by: notificationDisposeBag)
这是我的结构
struct NotificationSectionViewModel {
var header: String
var items: [AllNotificationModel]
}
extension NotificationSectionViewModel: SectionModelType {
typealias NotificationItem = AllNotificationModel
init(original: NotificationSectionViewModel, items: [AllNotificationModel]) {
self = original
self.items = items
}
}
这是数据模型
class AllNotificationModel : Codable {
let id, userID : Int
let title, body, createAt: String
enum CodingKeys: String, CodingKey {
case id, title, body
case userID = "user_id"
case createAt = "create at"
}
}
我想要达到的目标
需要这样的标题
“Today”: [
{
"id": 2421,
"user_id": 39,
"title": "todayNotification",
"body": "test",
"create at": "2021-02-26 17:33:44"
},
{
"id": 2349,
"user_id": 39,
"title": "check",
"body": "test",
"create at": "2021-02-26 09:36:05"
},
{
"id": 2206,
"user_id": 39,
"title": "New Deal",
"body": "new Deal 2",
"create at": "2021-02-26 13:43:16"
} ]
“Yesterday”: [
{
"id": 2134,
"user_id": 39,
"title": "Closed Deal",
"body": “deal deal”,
"create at": "2021-02-25 13:21:30"
} ]
“2021-02-24”: [
{
"id": 2134,
"user_id": 39,
"title": "Closed Deal",
"body": “deal”,
"create at": "2021-02-24 13:21:30"
},
{
"id": 2063,
"user_id": 39,
"title": "New Deal",
"body": "new Deal",
"create at": "2021-02-24 13:21:16"
}]
【问题讨论】:
标签: ios swift rx-swift rxdatasources