在tableView中使用RxSwift和可变部分时,每个部分仅显示一项

问题描述

我试图将三种类型的单元格放在一个tableView中,

使用rxSwift,第一个部分有两个项目,第二个有43个项目,最后一个有16个项目,

但是tableView每节仅显示一项

您可以在下图中看到错误

EXAMPLE OF THE ERROR

好的,这是代码

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",forCellReuseIdentifier: String(describing: TakeAwayCell.self))
     ordersTabel.register(UINib(nibName: "HistoryCell",forCellReuseIdentifier: String(describing: HistoryCell.self))

       ordersTabel.rx.willdisplayCell
        .subscribe(onNext: ({ (cell,indexPath) in
            let transform = CATransform3DTranslate(CATransform3DIdentity,-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>)
}

可以在图片代码中看到

tableView仅显示3个项目。

我试图做一个for循环,因为计数不止一个这样

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
                    }

我注意到,尽管计数超过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",forCellReuseIdentifier: String(describing: TakeAwayCell.self))
            ordersTabel.register(UINib(nibName: "HistoryCell",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
        }
    }
}