มุมมองตาราง RxSwift พร้อมเซลล์แบบกำหนดเองหลายประเภท

ฉันสงสัยว่ามีตัวอย่างโค้ดสำหรับ RxSwift เมื่อฉันสามารถใช้เซลล์ที่กำหนดเองหลายเซลล์ภายในมุมมองตารางเดียวได้หรือไม่ ตัวอย่างเช่น ฉันมีสองส่วนและส่วนแรกมี 10 เซลล์ที่มีประเภท CellWithImage identifier และส่วนที่สองมี 10 เซลล์ที่มีประเภท CellWithVideo identifier

ตัวอย่างการสอนและโค้ดทั้งหมดที่ฉันสร้างขึ้นใช้เซลล์ประเภทเดียวเท่านั้น เช่น RxSwiftTableViewExample

ขอบคุณสำหรับความช่วยเหลือใด ๆ


person edzio27    schedule 22.08.2016    source แหล่งที่มา


คำตอบ (3)


คุณสามารถตั้งค่าเซลล์ที่กำหนดเองได้หลายเซลล์โดยไม่มี RxDatasource

    //Register Cells as you want
    tableView.register(CustomRxTableViewCell.self, forCellReuseIdentifier: "Cell")
    tableView.register(UITableViewCell.self, forCellReuseIdentifier: "BasicCell")



    ViewModel.data.bind(to: tableView.rx.items){(tv, row, item) -> UITableViewCell in

        if row == 0 {
            let cell = tv.dequeueReusableCell(withIdentifier: "BasicCell", for: IndexPath.init(row: row, section: 0))

            cell.textLabel?.text = item.birthday
            return cell
        }else{
            let cell = tv.dequeueReusableCell(withIdentifier: "Cell", for: IndexPath.init(row: row, section: 0)) as! CustomRxTableViewCell
            cell.titleLb.text = item.name
            return cell
        }

    }.disposed(by: disposeBag)
person Hanryang    schedule 27.05.2019

ฉันจัดการโดยใช้ RxSwiftDataSources

ช่วยให้คุณสามารถใช้เซลล์แบบกำหนดเองที่มีหลายส่วนได้ ฉันใช้โค้ดนี้เพื่อขอความช่วยเหลือ

person edzio27    schedule 24.08.2016
comment
คุณสามารถแบ่งปันรหัสได้อย่างไรว่าคุณนำไปใช้อย่างไร? - person nburk; 22.11.2016
comment
ใช่ไหม ? เราไม่สามารถเพิ่มฟังก์ชั่นตารางอื่นโดยตรงกับ RxSwift ได้ใช่ไหม - person Jatin Garg; 07.07.2021

เผื่อใครสนใจ นี่คือการดำเนินการของฉัน ฉันมีแอพที่มีรายชื่อเกม ขึ้นอยู่กับว่าเกมจบหรือยังดำเนินต่อไป ฉันใช้เซลล์อื่น นี่คือรหัสของฉัน:

ใน ViewModel ฉันมีรายชื่อเกม แบ่งออกเป็นเกมที่เสร็จแล้ว/กำลังดำเนินการ และแมปเกมเหล่านั้นไปที่ SectionModel

let gameSections = PublishSubject<[SectionModel<String, Game>]>()
let dataSource = RxTableViewSectionedReloadDataSource<SectionModel<String, Game>>()

...

self.games.asObservable().map {[weak self] (games: [Game]) -> [SectionModel<String, Game>] in
    guard let safeSelf = self else {return []}
    safeSelf.ongoingGames = games.filter({$0.status != .finished})
    safeSelf.finishedGames = games.filter({$0.status == .finished})
    
    return [SectionModel(model: "Ongoing", items: safeSelf.ongoingGames), SectionModel(model: "Finished", items: safeSelf.finishedGames)]
}.bindTo(gameSections).addDisposableTo(bag)

จากนั้นใน ViewController ฉันผูกส่วนต่างๆ ของฉันเข้ากับมุมมองตาราง และใช้เซลล์ต่างๆ เช่นนี้ โปรดทราบว่าฉันสามารถใช้ indexPath เพื่อรับเซลล์ที่ถูกต้องแทนที่จะเป็นสถานะ

vm.gameSections.asObservable().bindTo(tableView.rx.items(dataSource: vm.dataSource)).addDisposableTo(bag)
vm.dataSource.configureCell = {[weak self] (datasource, tableview, indexpath, item) -> UITableViewCell in
    if item.status == .finished {
        let cell = tableview.dequeueReusableCell(withIdentifier: "FinishedGameCell", for: indexpath) as! FinishedGameCell
        cell.nameLabel.text = item.opponent.shortName
        return cell
    } else {
        let cell = tableview.dequeueReusableCell(withIdentifier: "OnGoingGameCell", for: indexpath) as! OnGoingGameCell
        cell.titleLabel.text = item.opponent.shortName
        return cell
    }
}
person streem    schedule 09.01.2017
comment
คุณจะอัปเดตรายการในแหล่งข้อมูลได้อย่างไร dataSource.sectionModels เป็นการรับเท่านั้น โมเดลของฉันคืออาร์เรย์ และฉันกำลังพยายามอัปเดตรายการใดรายการหนึ่งหลังจากที่ผู้ใช้ป้อนข้อมูลบางส่วน ขอบคุณ - person MikeG; 02.10.2018