使用UITableViewDiffableDataSource创建节标题

问题描述 投票:1回答:1

我对UITableViewDiffableDataSource进行了一些修改,并且能够毫无问题地加载tableView。我正在尝试在tableView中创建节标题,但是遇到了一些易燃的行为。

该部分枚举枚举定义如下:

    enum AlertLevel: String, Codable, CaseIterable {
        case green = "green"
        case yellow = "yellow"
        case orange = "orange"
        case red = "red"
    }

这是我对tableView(viewForHeaderInSection:)的实现

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let label = UILabel()
        label.text = dataSource.snapshot().sectionIdentifiers[section].rawValue.capitalized
        label.textColor = UIColor.black

        return label
    }

这给了我4个标签,这些标签堆叠在tableView顶部的标题单元格中。

[我将Dash发射到RTFD,然后看到tableView(titleForHeaderInSection:)是换皮那只猫的另一种方法。所以我把它扔进去了,]:>

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return dataSource.snapshot().sectionIdentifiers[section].rawValue.capitalized
    }

我在其中插入了一个断点,但它从未被击中。因此,我实现了tableView(heightForHeaderInSection:),并且标头得到更新,但标头没有显示任何字符串。

该表加载LOT的速度比使用IndexPaths的“老式方式”快(我正在使用USGS地震数据库学习TableViewDiffableDataSource,但无法显示标题。

任何人都知道如何使部分在TableViewDiffableDataSource上正常工作?我很难相信他们会在没有这种基本功能的情况下放任自流,所以我只能得出结论,我正在弄污某些东西……我不知道:)

哦...这就是我定义数据源的方式:

func makeDataSource() -> UITableViewDiffableDataSource<AlertLevel, Earthquake> {
    return UITableViewDiffableDataSource(tableView: tableView) { tableView, indexPath, earthquake in
        let cell = tableView.dequeueReusableCell(withIdentifier: self.reuseID, for: indexPath)
        cell.textLabel?.text = earthquake.properties.title
        cell.detailTextLabel?.text = earthquake.properties.detail

        return cell
    }
}

我对UITableViewDiffableDataSource进行了一些修改,并且能够毫无问题地加载tableView。我正在尝试在tableView中创建节标题,但是我遇到了一些...

ios swift uitableview diff diffabledatasource
1个回答
0
投票

通过这样子类化UITableViewDiffableDataSource类,我能够做到这一点:

© www.soinside.com 2019 - 2024. All rights reserved.