UITableViewDiffableDataSource:如何获取部分索引
UITableViewDiffableDataSource: how to get a section index
我正在尝试采用新的 iOS 13 UITableViewDiffableDataSource,但遇到了麻烦;我不知道如何实现
func sectionIndexTitles(for tableView: UITableView) -> [String]?
那是数据源方法,不是委托方法。因此,既然数据源是 UITableViewDiffableDataSource,就需要实现该方法。但事实并非如此。
我尝试继承 UITableViewDiffableDataSource 并添加 sectionIndexTitles
的实现,但从未调用过我的实现:
class MyDataSource : UITableViewDiffableDataSource<String,String> {
func sectionIndexTitles(for tableView: UITableView) -> [String]? {
return self.snapshot().sectionIdentifiers // not called
}
}
有人解决过这个问题吗?为了以防万一,我会将其作为错误归档。
您需要继承 UITableViewDiffableDataSource
然后覆盖
func sectionIndexTitles(for tableView: UITableView)
靠自己。
不过要启用索引功能,您还必须覆盖
func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int)
这是我如何实现它的示例:
import UIKit
import MediaPlayer
class TableViewDiffableDataSource: UITableViewDiffableDataSource<String, MPMediaItemCollection> {
var sectionTitles = [String]()
override func sectionIndexTitles(for tableView: UITableView) -> [String]? {
return sectionTitles
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sectionTitles[section]
}
override func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
return sectionTitles.firstIndex(of: title) ?? 0
}
}
Credits go to Steve Breen, who led me in the right direction.
将 self.dataSource
初始化为 UITableViewDiffableDataSource
(将自身设置为 tableView.dataSource
)后,将 tableView.dataSource
设置回自身,即 UITableViewController
子类.现在在你的 numberOfSectionsInTableView
和 numberOfRowsInSection
方法中将它们转发给 self.dataSource
和 return 它的信息(这是组合模式)。现在您的 UITableViewController
只是正常实现其部分标题,因为它是 table 的数据源。
我相信 UITableViewDiffableDataSource
不应该将自己设置为数据源,如果已经设置的话,但我猜他们将其设计为以最不容易出错的方式工作,因为将 UITableViewController
添加到故事板它默认设置为 dataSource
和 delegate
。
我正在尝试采用新的 iOS 13 UITableViewDiffableDataSource,但遇到了麻烦;我不知道如何实现
func sectionIndexTitles(for tableView: UITableView) -> [String]?
那是数据源方法,不是委托方法。因此,既然数据源是 UITableViewDiffableDataSource,就需要实现该方法。但事实并非如此。
我尝试继承 UITableViewDiffableDataSource 并添加 sectionIndexTitles
的实现,但从未调用过我的实现:
class MyDataSource : UITableViewDiffableDataSource<String,String> {
func sectionIndexTitles(for tableView: UITableView) -> [String]? {
return self.snapshot().sectionIdentifiers // not called
}
}
有人解决过这个问题吗?为了以防万一,我会将其作为错误归档。
您需要继承 UITableViewDiffableDataSource
然后覆盖
func sectionIndexTitles(for tableView: UITableView)
靠自己。
不过要启用索引功能,您还必须覆盖
func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int)
这是我如何实现它的示例:
import UIKit
import MediaPlayer
class TableViewDiffableDataSource: UITableViewDiffableDataSource<String, MPMediaItemCollection> {
var sectionTitles = [String]()
override func sectionIndexTitles(for tableView: UITableView) -> [String]? {
return sectionTitles
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sectionTitles[section]
}
override func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
return sectionTitles.firstIndex(of: title) ?? 0
}
}
Credits go to Steve Breen, who led me in the right direction.
将 self.dataSource
初始化为 UITableViewDiffableDataSource
(将自身设置为 tableView.dataSource
)后,将 tableView.dataSource
设置回自身,即 UITableViewController
子类.现在在你的 numberOfSectionsInTableView
和 numberOfRowsInSection
方法中将它们转发给 self.dataSource
和 return 它的信息(这是组合模式)。现在您的 UITableViewController
只是正常实现其部分标题,因为它是 table 的数据源。
我相信 UITableViewDiffableDataSource
不应该将自己设置为数据源,如果已经设置的话,但我猜他们将其设计为以最不容易出错的方式工作,因为将 UITableViewController
添加到故事板它默认设置为 dataSource
和 delegate
。