Implementando múltiples secciones con RxDatasources
Estoy intentando hacer múltiples secciones (dos en realidad) usando RxDatasources. Normalmente, con una sección, lo haría de la siguiente manera:
Modelo de sección:
import Foundation
import RxDataSources
typealias NotificationSectionModel = AnimatableSectionModel<string, notificationcellmodel="">
struct NotificationCellModel: Equatable, IdentifiableType {
static func == (lhs: NotificationCellModel, rhs: NotificationCellModel) -> Bool {
return lhs.model.id == rhs.model.id
}
var identity: String {
return model.id
}
var model: NotificationModel
var cellIdentifier = "NotificationTableViewCell"
}
Luego, el modelo real:
struct NotificationModel: Codable, Equatable {
let body: String
let title: String
let id: String
}
Y lo usaría así (en el controlador de vista):
private func observeTableView() {
let dataSource = RxTableViewSectionedAnimatedDataSource<notificationsectionmodel>(
configureCell: { dataSource, tableView, indexPath, item in
if let cell = tableView.dequeueReusableCell(withIdentifier: item.cellIdentifier, for: indexPath) as? BaseTableViewCell {
cell.setup(data: item.model)
return cell
}
return UITableViewCell()
}
)
notificationsViewModel.notifications
.map { notifications -> [NotificationCellModel] in
return notifications.map { NotificationCellModel(model: $0, cellIdentifier: NotificationTableViewCell.identifier) }
}
.map { [NotificationSectionModel(model: "", items: $0)] }
.bind(to: self.tableView.rx.items(dataSource: dataSource))
.disposed(by: disposeBag)
}
Pero, ¿cómo lo haría con múltiples secciones, con diferentes tipos de modelos/celdas?</string,>
davy.ai
Para crear varias secciones con diferentes tipos de modelos/celdas utilizando RxDatasources, puedes seguir estos pasos:
SectionedDataSourceType
, especificando cómo configurar las celdas para cada sección:Asegúrate de definir los modelos y celdas para cada sección apropiadamente en el código anterior.
Nota: si quieres usar
RxTableViewSectionedAnimatedDataSource
en lugar deRxTableViewSectionedReloadDataSource
, puedes hacerlo siguiendo pasos similares y modificando el cierreconfigureCell
en consecuencia.</string,>