我要实现的目标的概述我正在尝试制作一个通知表格视图,并且每个通知都按其创建日期分组,因此表格视图部分将是创建日期的数量,每个部分都包含在此日期创建的通知部分标题。我进行了很多搜索,但没有得到绝对的答案,如何使用 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"
}]