Ios 在tableView中使用RxSwift和可变节时,每个节仅显示一项
我试图在一个表视图中放置三种类型的单元格 通过使用rxSwift,第一节有两项,第二节有43项,最后一节有16项 但是tableView每个部分只显示一个项目 您可以在下图中看到错误 好的,这是密码Ios 在tableView中使用RxSwift和可变节时,每个节仅显示一项,ios,swift,xcode,rx-swift,Ios,Swift,Xcode,Rx Swift,我试图在一个表视图中放置三种类型的单元格 通过使用rxSwift,第一节有两项,第二节有43项,最后一节有16项 但是tableView每个部分只显示一个项目 您可以在下图中看到错误 好的,这是密码 class OrdersVC: BaseViewController { @IBOutlet weak var ordersTabel: UITableView! public let orders : PublishSubject<OrdersTypeModel>
class OrdersVC: BaseViewController {
@IBOutlet weak var ordersTabel: UITableView!
public let orders : PublishSubject<OrdersTypeModel> = PublishSubject()
var ordersDecoded = OrdersTypeModel()
var delivery = Array<DetailsModel>()
var takeAway = Array<DetailsModel>()
var history = Array<OrderModel>()
let disposeBag = DisposeBag()
override func viewDidLoad() {
super.viewDidLoad()
ordersTabel.allowsSelection = false
setupBindings()
}
private func setupBindings() {
ordersTabel.register(UINib(nibName: "DeliveryCell", bundle: nil), forCellReuseIdentifier: String(describing: DeliveryCell.self))
ordersTabel.register(UINib(nibName: "TakeAwayCell", bundle: nil), forCellReuseIdentifier: String(describing: TakeAwayCell.self))
ordersTabel.register(UINib(nibName: "HistoryCell", bundle: nil), forCellReuseIdentifier: String(describing: HistoryCell.self))
ordersTabel.rx.willDisplayCell
.subscribe(onNext: ({ (cell,indexPath) in
let transform = CATransform3DTranslate(CATransform3DIdentity, 0, -250, 0)
cell.layer.transform = transform
UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.5, options: .curveEaseOut, animations: {
cell.alpha = 1
cell.layer.transform = CATransform3DIdentity
}, completion: nil)
})).disposed(by: disposeBag)
orders
.observeOn(MainScheduler.instance)
.subscribe(
onNext: { [self] in
print("ononon onNext: \($0)")
ordersDecoded = $0
delivery = ordersDecoded.orderdelivery
delivery2.onNext(delivery)
takeAway = ordersDecoded.ordertakeaway
takeAway2.onNext(takeAway)
history = ordersDecoded.orders1
history2.onNext(history)
print("ononon onNext: \(delivery.count), \(takeAway.count), \(history.count)")
let sections: [MultipleSectionModel] = [
.DeliverySection(title: "Delivery", items: [.DeliveryItem(model: delivery)]),
.TakeAwaySection(title: "Take Away", items: [.TakeAwayItem(model: takeAway)]),
.HistorySection(title: "History", items: [.HistoryItem(model: history)])
]
let dataSource = OrdersVC.dataSource()
Observable.just(sections)
.bind(to: ordersTabel.rx.items(dataSource: dataSource))
.disposed(by: disposeBag)
},
onError: { print("ononon onError: \($0)") },
onCompleted: { print("ononon onCompleted") },
onDisposed: { print("ononon onDisposed") }
)
.disposed(by: disposeBag)
}
}
extension OrdersVC {
static func dataSource() -> RxTableViewSectionedReloadDataSource<MultipleSectionModel> {
return RxTableViewSectionedReloadDataSource<MultipleSectionModel>(
configureCell: { dataSource, table, idxPath, _ in
print("sjdkhbgdjkf \(idxPath)")
print("sjdkhbgdjkf \(idxPath.row)")
switch dataSource[idxPath] {
case let .DeliveryItem(model):
print("sjdkhbgdjkf1 \(model.count)")
let cell: DeliveryCell = table.dequeueReusableCell(withIdentifier: "DeliveryCell", for: idxPath) as! DeliveryCell
cell.model = model[idxPath.row]
return cell
case let .TakeAwayItem(model):
print("sjdkhbgdjkf2 \(model.count)")
for i in 0 ... model.count - 1 {
let cell: TakeAwayCell = table.dequeueReusableCell(withIdentifier: "TakeAwayCell", for: idxPath) as! TakeAwayCell
cell.model = model[i]
print("sjdkhbgdjkf2 \(i)")
return cell
}
case let .HistoryItem(model):
print("sjdkhbgdjkf3 \(model.count)")
for i in 0 ... model.count - 1 {
let cell: HistoryCell = table.dequeueReusableCell(withIdentifier: "HistoryCell", for: idxPath) as! HistoryCell
cell.model = model[i]
print("sjdkhbgdjkf3 \(i)")
return cell
}
}
return table.dequeueReusableCell(withIdentifier: "HistoryCell", for: idxPath) as! HistoryCell
},
titleForHeaderInSection: { dataSource, index in
let section = dataSource[index]
if section.title == "History" {
return section.title
} else {
return nil
}
}
)
}
}
enum MultipleSectionModel {
case DeliverySection(title: String, items: [SectionItem])
case TakeAwaySection(title: String, items: [SectionItem])
case HistorySection(title: String, items: [SectionItem])
}
extension MultipleSectionModel: SectionModelType {
typealias Item = SectionItem
var items: [SectionItem] {
switch self {
case .DeliverySection(title: _, items: let items):
return items.map { $0 }
case .TakeAwaySection(title: _, items: let items):
return items.map { $0 }
case .HistorySection(title: _, items: let items):
return items.map { $0 }
}
}
init(original: MultipleSectionModel, items: [Item]) {
switch original {
case let .DeliverySection(title: title, items: _):
self = .DeliverySection(title: title, items: items)
case let .TakeAwaySection(title: title, items: _):
self = .TakeAwaySection(title: title, items: items)
case let .HistorySection(title, _):
self = .HistorySection(title: title, items: items)
}
}
}
extension MultipleSectionModel {
var title: String {
switch self {
case .DeliverySection(title: let title, items: _):
return title
case .TakeAwaySection(title: let title, items: _):
return title
case .HistorySection(title: let title, items: _):
return title
}
}
}
enum SectionItem {
case DeliveryItem(model: Array<DetailsModel>)
case TakeAwayItem(model: Array<DetailsModel>)
case HistoryItem(model: Array<OrderModel>)
}
我注意到它不执行循环,尽管计数大于1
感谢您的帮助您使您的节模型变得比需要的更复杂,我猜结果是,您开始千方百计地尝试编译其余部分。这是您重新编写的代码,它应该为您提供一个更好的起点
final class OrdersVC: BaseViewController {
@IBOutlet weak var ordersTabel: UITableView! {
didSet {
ordersTabel.allowsSelection = false
ordersTabel.register(UINib(nibName: "DeliveryCell", bundle: nil), forCellReuseIdentifier: String(describing: DeliveryCell.self))
ordersTabel.register(UINib(nibName: "TakeAwayCell", bundle: nil), forCellReuseIdentifier: String(describing: TakeAwayCell.self))
ordersTabel.register(UINib(nibName: "HistoryCell", bundle: nil), forCellReuseIdentifier: String(describing: HistoryCell.self))
}
}
let orders = PublishSubject<OrdersTypeModel>()
private let disposeBag = DisposeBag()
override func viewDidLoad() {
super.viewDidLoad()
setupBindings()
}
private func setupBindings() {
orders
.map { (ordersDecoded) in
return [
MultipleSectionModel(model: "Delivery", items: ordersDecoded.orderdelivery.map { .deliveryItem(model: $0) }),
MultipleSectionModel(model: "Take Away", items: ordersDecoded.ordertakeaway.map { .takeAwayItem(model: $0) }),
MultipleSectionModel(model: "History", items: ordersDecoded.orders1.map { .historyItem(model: $0) })
]
}
.observeOn(MainScheduler.instance)
.bind(to: ordersTabel.rx.items(dataSource: OrdersVC.dataSource()))
.disposed(by: disposeBag)
}
}
extension OrdersVC {
static func dataSource() -> RxTableViewSectionedReloadDataSource<MultipleSectionModel> {
return RxTableViewSectionedReloadDataSource<MultipleSectionModel>(
configureCell: { _, table, idxPath, item in
return item.cell(table: table, idxPath: idxPath)
},
titleForHeaderInSection: { dataSource, index in
let section = dataSource[index]
return section.model == "History" ? section.model : nil
}
)
}
}
typealias MultipleSectionModel = SectionModel<String, SectionItem>
enum SectionItem {
case deliveryItem(model: DetailsModel)
case takeAwayItem(model: DetailsModel)
case historyItem(model: OrderModel)
}
extension SectionItem {
func cell(table: UITableView, idxPath: IndexPath) -> UITableViewCell {
switch self {
case let .deliveryItem(model):
let cell: DeliveryCell = table.dequeueReusableCell(withIdentifier: "DeliveryCell", for: idxPath) as! DeliveryCell
cell.model = model
return cell
case let .takeAwayItem(model):
let cell: TakeAwayCell = table.dequeueReusableCell(withIdentifier: "TakeAwayCell", for: idxPath) as! TakeAwayCell
cell.model = model
return cell
case let .historyItem(model):
let cell: HistoryCell = table.dequeueReusableCell(withIdentifier: "HistoryCell", for: idxPath) as! HistoryCell
cell.model = model
return cell
}
}
}
final类OrdersVC:BaseViewController{
@IBMOutlet弱var ordersTabel:UITableView{
迪塞特{
ordersTabel.AllowSelection=false
register(UINib(nibName:“DeliveryCell”,bundle:nil),forCellReuseIdentifier:String(描述:DeliveryCell.self))
register(UINib(nibName:“TakeAwayCell”,bundle:nil),forCellReuseIdentifier:String(description:TakeAwayCell.self))
register(UINib(nibName:“HistoryCell”,bundle:nil),forCellReuseIdentifier:String(description:HistoryCell.self))
}
}
let orders=PublishSubject()
私有出租dispebag=dispebag()
重写func viewDidLoad(){
super.viewDidLoad()
setupBindings()
}
专用函数设置绑定(){
命令
.map{(ordersDecoded)中的
返回[
MultipleSectionModel(模型:“交付”,项:ordersDecoded.orderdelivery.map{.deliveryItem(模型:$0)}),
MultipleSectionModel(模型:“Takeaway”,项:ordersDecoded.ordertakeaway.map{.takeAwayItem(模型:$0)}),
MultipleSectionModel(模型:“历史”,项:ordersDecoded.orders1.map{.historyItem(模型:$0)})
]
}
.observeOn(MainScheduler.instance)
.bind(到:ordersTabel.rx.items(数据源:OrdersVC.dataSource())
.处置(由:处置人)
}
}
扩展指令svc{
静态func dataSource()->RxTableViewSectionedReloadDataSource{
返回RxTableViewSectionedReloadDataSource(
configureCell:{},表,idxPath,中的项
返回item.cell(表:表,idxPath:idxPath)
},
titleForHeaderInSection:{dataSource,中的索引
let section=数据源[索引]
返回section.model==“历史”?section.model:nil
}
)
}
}
typealias MultipleSectionModel=SectionModel
枚举节项{
案例交付项目(型号:DetailsModel)
案例外卖项目(型号:DetailsModel)
案例历史项目(模型:OrderModel)
}
扩展部分项目{
func单元格(表:UITableView,idxPath:indepath)->UITableViewCell{
切换自身{
案例let.deliveryItem(型号):
让cell:DeliveryCell=table.dequeueReusableCell(带有标识符:“DeliveryCell”,for:idxPath)作为!DeliveryCell
cell.model=模型
返回单元
外卖项目(型号):
让cell:TakeAwayCell=table.dequeueReusableCell(标识符为:“TakeAwayCell”,for:idxPath)作为!TakeAwayCell
cell.model=模型
返回单元
案例let.历史项目(模型):
让cell:HistoryCell=table.dequeueReusableCell(带有标识符:“HistoryCell”,for:idxPath)作为!HistoryCell
cell.model=模型
返回单元
}
}
}
您使您的节模型变得比需要的更复杂,我猜结果是,您开始千方百计地尝试编译其余部分。这是您重新编写的代码,它应该为您提供一个更好的起点
final class OrdersVC: BaseViewController {
@IBOutlet weak var ordersTabel: UITableView! {
didSet {
ordersTabel.allowsSelection = false
ordersTabel.register(UINib(nibName: "DeliveryCell", bundle: nil), forCellReuseIdentifier: String(describing: DeliveryCell.self))
ordersTabel.register(UINib(nibName: "TakeAwayCell", bundle: nil), forCellReuseIdentifier: String(describing: TakeAwayCell.self))
ordersTabel.register(UINib(nibName: "HistoryCell", bundle: nil), forCellReuseIdentifier: String(describing: HistoryCell.self))
}
}
let orders = PublishSubject<OrdersTypeModel>()
private let disposeBag = DisposeBag()
override func viewDidLoad() {
super.viewDidLoad()
setupBindings()
}
private func setupBindings() {
orders
.map { (ordersDecoded) in
return [
MultipleSectionModel(model: "Delivery", items: ordersDecoded.orderdelivery.map { .deliveryItem(model: $0) }),
MultipleSectionModel(model: "Take Away", items: ordersDecoded.ordertakeaway.map { .takeAwayItem(model: $0) }),
MultipleSectionModel(model: "History", items: ordersDecoded.orders1.map { .historyItem(model: $0) })
]
}
.observeOn(MainScheduler.instance)
.bind(to: ordersTabel.rx.items(dataSource: OrdersVC.dataSource()))
.disposed(by: disposeBag)
}
}
extension OrdersVC {
static func dataSource() -> RxTableViewSectionedReloadDataSource<MultipleSectionModel> {
return RxTableViewSectionedReloadDataSource<MultipleSectionModel>(
configureCell: { _, table, idxPath, item in
return item.cell(table: table, idxPath: idxPath)
},
titleForHeaderInSection: { dataSource, index in
let section = dataSource[index]
return section.model == "History" ? section.model : nil
}
)
}
}
typealias MultipleSectionModel = SectionModel<String, SectionItem>
enum SectionItem {
case deliveryItem(model: DetailsModel)
case takeAwayItem(model: DetailsModel)
case historyItem(model: OrderModel)
}
extension SectionItem {
func cell(table: UITableView, idxPath: IndexPath) -> UITableViewCell {
switch self {
case let .deliveryItem(model):
let cell: DeliveryCell = table.dequeueReusableCell(withIdentifier: "DeliveryCell", for: idxPath) as! DeliveryCell
cell.model = model
return cell
case let .takeAwayItem(model):
let cell: TakeAwayCell = table.dequeueReusableCell(withIdentifier: "TakeAwayCell", for: idxPath) as! TakeAwayCell
cell.model = model
return cell
case let .historyItem(model):
let cell: HistoryCell = table.dequeueReusableCell(withIdentifier: "HistoryCell", for: idxPath) as! HistoryCell
cell.model = model
return cell
}
}
}
final类OrdersVC:BaseViewController{
@IBMOutlet弱var ordersTabel:UITableView{
迪塞特{
ordersTabel.AllowSelection=false
register(UINib(nibName:“DeliveryCell”,bundle:nil),forCellReuseIdentifier:String(描述:DeliveryCell.self))
register(UINib(nibName:“TakeAwayCell”,bundle:nil),forCellReuseIdentifier:String(description:TakeAwayCell.self))
register(UINib(nibName:“HistoryCell”,bundle:nil),forCellReuseIdentifier:String(description:HistoryCell.self))
}
}
let orders=PublishSubject()
私有出租dispebag=dispebag()
重写func viewDidLoad(){
super.viewDidLoad()
setupBindings()
}
专用函数设置绑定(){
命令
.map{(ordersDecoded)中的
返回[
MultipleSectionModel(模型:“交付”,项:ordersDecoded.orderdelivery.map{.deliveryItem(模型:$0)}),
MultipleSectionModel(模型:“Takeaway”,项:ordersDecoded.ordertakeaway.map{.takeAwayItem(模型:$0)}),
MultipleSectionModel(模型:“历史”,项:ordersDecoded.orders1.map{.historyItem(模型:$0)})
]
}
.observeOn(MainScheduler.instance)
.bind(到:ordersTabel.rx.items(数据源:OrdersVC.dataSource())
.处置(由:处置人)
}
}
扩展指令svc{
静态func dataSource()->RxTableViewSectionedReloadDataSource{
返回RxTableViewSectionedReloadDataSource(
configureCell:{},表,idxPath,中的项
返回item.cell(表:表,idxPath:idxPath)
},
titleForHeaderInSection:{dataSource,中的索引
let section=数据源[索引]
返回section.model==“历史”?section.model:nil
}
)
}
}
typealias MultipleSectionModel=SectionModel
EN