Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/95.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用UITableViewDiffableDataSource创建节标题_Ios_Swift_Uitableview_Diff_Diffabledatasource - Fatal编程技术网

Ios 使用UITableViewDiffableDataSource创建节标题

Ios 使用UITableViewDiffableDataSource创建节标题,ios,swift,uitableview,diff,diffabledatasource,Ios,Swift,Uitableview,Diff,Diffabledatasource,我正在对UITableViewDiffableDataSource进行一些修补,并且我能够毫无问题地加载一个tableView。我试图在tableView中创建节标题,但是遇到了一些不规则的行为 部分enum enum定义如下: enum AlertLevel:字符串、可编码、可大小写{ case green=“绿色” case yellow=“黄色” case orange=“橙色” case red=“红色” } 这是我对tableView(viewForHeaderInSection:)

我正在对
UITableViewDiffableDataSource
进行一些修补,并且我能够毫无问题地加载一个tableView。我试图在tableView中创建节标题,但是遇到了一些不规则的行为

部分enum enum定义如下:

enum AlertLevel:字符串、可编码、可大小写{
case green=“绿色”
case yellow=“黄色”
case orange=“橙色”
case red=“红色”
}
这是我对
tableView(viewForHeaderInSection:)的实现

func tableView(tableView:UITableView,viewForHeaderInSection:Int)->UIView?{
let label=UILabel()
label.text=dataSource.snapshot().sectionIdentifiers[section].rawValue.capitalized
label.textColor=UIColor.black
退货标签
}
这就给了我4个标签,它们堆叠在表格视图顶部的标题单元格中

我向RTFD启动了Dash,我看到了
tableView(标题ForHeaderSection:)
是另一种剥猫皮的方法。所以我把这个放进去了,取而代之的是:

func tableView(tableView:UITableView,titleForHeaderInSection:Int)->字符串?{
返回dataSource.snapshot().sectionIdentifiers[section].rawValue.capitalized
}
我投了一个断点,但它从未被击中。因此,我实现了
tableView(heightForHeaderInSection:)
并更新了标题,但没有显示标题的字符串

该表的加载速度比使用
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
    }
}
func makeDataSource()->UITableViewDiffableDataSource{
返回UITableViewDiffableDataSource(tableView:tableView){tableView,indexPath,中的地震
let cell=tableView.dequeueReusableCell(带标识符:self.reuseID,for:indexath)
cell.textLabel?.text=地震.properties.title
cell.detailTextLabel?.text=地震.properties.detail
返回单元
}
}

我可以通过如下子类化
UITableViewDiffableDataSource
类来实现这一点:

类MyDataSource:UITableViewDiffableDataSource{
重写func tableView(tableView:UITableView,titleForHeaderInSection:Int)->String{
让section=self.snapshot().sectionIdentifiers[section]
返回节.header
}
}
其中您的
部分是:

enum节:Int{
案例一
变量头:字符串{
切换自身{
案例一:返回“标题一”
}
}
}
然后按以下方式分配新创建的数据源:

dataSource=MyDataSource

也就是说,您不再需要使用
UITableViewDiffableDataSource
,而是使用一个子类
MyDataSource
类。

检查您是否只使用了其中一种方法来设置节头。所以要么使用titleForHeaderInSection,要么使用viewForHeaderInSection,但不要同时使用这两种方法。是的,我尝试了一种方法,但都不起作用。另一种明显的方式也没有。我想我会成为Cortes,用这个“烧掉飞船”,然后全力以赴使用SwiftUI、Combine等。HeightForHeaderSection是一个委托方法。这就是为什么它会被叫来。未调用TitleForHeaderSection,因为它是UITableViewDataSource协议的数据源方法。它不起作用,因为您使用的是DiffableDataSource。你不能同时使用这两种语言。这将为所有语言返回“Hello World!”。实现中可能缺少的任何其他花絮?是的,我可以回答,但我不确定您在寻找什么信息。你能具体说明一下吗?在这个方法中,您可以获得itemIdentifier,sectionIndex。我认为这两种情况在90%的情况下都应该涵盖用例。但是如果还需要什么,请询问@AdrianIn如果您还没有弄明白,我将建议如何为每个部分获得不同的标题:我在枚举中有我的部分ID。在my
UITableViewDiffableDataSource
子类的
titleForHeaderInSection
中,我使用
self.snapshot().sectionIdentifiers[section]
获取当前节,切换该节,并返回该节的正确标题。就像海棠派一样简单!如何在不初始化的情况下分配数据源。dataSource=MyDataSource