0

我想将可扩展的 tableView 从默认值传递给 Rx,但我发现我无法正确使用numberOfRownInSection的问题。

现在的逻辑是......当你的结构有标志isExpandable = false时,行数为0

我所拥有的是带有静态数据的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)
4

1 回答 1

2

尝试查看 GitHub 上的 RxDataSources 存储库。有一个关于如何执行此操作的示例。您可以通过 Rx 方式将 cellViewModels 绑定到 tableview

于 2019-12-18T13:54:06.003 回答