簡體   English   中英

Swift 5 RxDataSource 可擴展單元

[英]Swift 5 RxDataSource expandable cells

我想將可擴展的 tableView 從默認值傳遞給 Rx,但我發現我無法正確使用numberOfRownInSection的問題。

現在的邏輯是......當你的結構有標志isExpandable = false時,行數為0

我所擁有的是帶有 static 數據的tableView (結構將在下面提供)。 HeaderView 作為標題,單元格作為可擴展內容。

切換功能:

func toggleCell(_ section: Int) {

    var indexPaths = [IndexPath]()

    for row in data[section].items.indices {
        let indexPath = IndexPath(row: row, section: section)
        indexPaths.append(indexPath)
    }

    let isExpanded = data[section].isExpanded
    data[section].isExpanded = !isExpanded

    if isExpanded {
        categoriesTableView.deleteRows(at: indexPaths, with: .none)
    } else {
        categoriesTableView.insertRows(at: indexPaths, with: .none)
    }

}

默認 tableView 委托:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell: HomeViewCell = tableView.dequeueReusableCell(forIndexPath: indexPath)
    cell.categoryLabel.text = data[indexPath.section].items[indexPath.row].title ?? ""
    return cell
}

func numberOfSections(in tableView: UITableView) -> Int {
    return data.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if !data[section].isExpanded {
        return 0
    }

    return data[section].items.count
}

結構

var data: [CategoriesSectionData] = [CategoriesSectionData(header: "Elektro",            items: [CategoriesCellData(price: 12.99, title: "Gärtner 1"),
                                                                                                 CategoriesCellData(price: 15.30, title: "Gärtner 2"),
                                                                                                 CategoriesCellData(price: 25.99, title: "Gärtner 3")], isExpanded: false, icon: "home_menu_1"),
                                     CategoriesSectionData(header: "Gartenpflege",       items: [CategoriesCellData(price: 14.0, title: "Gärtner 1")], isExpanded: false, icon: "home_menu_2"),
                                     CategoriesSectionData(header: "Sanitär",            items: [], isExpanded: false, icon: "home_menu_3"),
                                     CategoriesSectionData(header: "Hausmeisterdienste", items: [], isExpanded: false, icon: "home_menu_4"),
                                     CategoriesSectionData(header: "Meisterprüfung",     items: [], isExpanded: false, icon: "home_menu_3"),
                                     CategoriesSectionData(header: "Gartenpflege",       items: [], isExpanded: false, icon: "home_menu_2"),
                                     CategoriesSectionData(header: "Sanitär",            items: [], isExpanded: false, icon: "home_menu_1"),
                                     CategoriesSectionData(header: "Hausmeisterdienste", items: [], isExpanded: false, icon: "home_menu_4")]

要將其傳遞給 Rx,我使用了以下代碼,但在插入/刪除行時出現錯誤,因為我不想清除我的 dataSource 並且無法將numberOfRownInSection設置為不顯示帶有可擴展標志的單元格。

let dataSource = RxTableViewSectionedReloadDataSource<CategoriesSectionData>(
        configureCell: { ds, tv, indexPath, item in

            let cell: HomeViewCell = tv.dequeueReusableCell(forIndexPath: indexPath)
            cell.categoryLabel.text = item.title ?? ""
            return cell
        },

        titleForHeaderInSection: { ds, index in
            return ds.sectionModels[index].header
        }
    )

    self.dataSource = dataSource

    Observable.just(data)
        .bind(to: categoriesTableView.rx.items(dataSource: dataSource))
        .disposed(by: disposeBag)

    categoriesTableView.rx
        .setDelegate(self)
        .disposed(by: disposeBag)

嘗試查看 GitHub 上的 RxDataSources 存儲庫。 有一個關於如何執行此操作的示例。 您可以通過 Rx 方式將 cellViewModels 綁定到 tableview

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM