Обзор того, чего я пытаюсь достичь Я пытаюсь создать табличное представление уведомлений, и каждое уведомление группируется по дате создания, поэтому в разделах табличного представления будет число созданной даты, каждый раздел с уведомлениями, созданными на эту дату в заголовок раздела. Я много искал, но не получил однозначного ответа, как с помощью 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"
}]