如何在 collection 视图中添加 indexTitles? (iOS 10+)
How to add indexTitles in a collection view? (iOS 10+)
作为问题,我正在尝试在 collection 视图中添加“索引标题”,以便快速导航我的 UIColletionView
.
的内容
我读过,从 iOS 10 开始,像 indexTitles(for:)
和 collectionView(_:indexPathForIndexTitle:at:)
这样的方法可以帮助我,就像 UITableViews.但我没有得到任何结果!你知道如何使用这些方法了吗?在此先感谢您的帮助。
如果您查看 UICollectionView.h
,indexTitlesForCollectionView
仅适用于 tvos:
/// Returns a list of index titles to display in the index view (e.g. ["A", "B", "C" ... "Z", "#"])
- (nullable NSArray<NSString *> *)indexTitlesForCollectionView:(UICollectionView *)collectionView API_AVAILABLE(tvos(10.2));
您可能已经阅读了 Apple 的文档,其中指出 indexTitle 方法可用于 iOS 10.3+ 和 tvOS 10.2+:https://developer.apple.com/documentation/uikit/uicollectionviewdatasource/2851455-indextitles
我尝试在 iOS 上实现它,但它不起作用,这让我相信他们在文档中不知何故犯了一个错误。
我只是通过将以下内容添加到我的数据源来获得在 iPhone 上运行的标题:
func indexTitles(for collectionView: UICollectionView) -> [String]? {
guard !RunUtilities.isIpad else { return nil }
return Array(Set(objects.map{ String([=10=].name.prefix(1)) })).sorted(by: { [=10=] < })
}
func collectionView(_ collectionView: UICollectionView, indexPathForIndexTitle title: String, at index: Int) -> IndexPath {
guard let index = objects.firstIndex(where: { [=10=].name.prefix(1) == title }) else {
return IndexPath(item: 0, section: 0)
}
return IndexPath(item: index, section: 0)
}
编辑:看起来这是从 iOS 14 开始工作的。我安装了 13.4 运行时但标题不显示。
作为问题,我正在尝试在 collection 视图中添加“索引标题”,以便快速导航我的 UIColletionView
.
我读过,从 iOS 10 开始,像 indexTitles(for:)
和 collectionView(_:indexPathForIndexTitle:at:)
这样的方法可以帮助我,就像 UITableViews.但我没有得到任何结果!你知道如何使用这些方法了吗?在此先感谢您的帮助。
如果您查看 UICollectionView.h
,indexTitlesForCollectionView
仅适用于 tvos:
/// Returns a list of index titles to display in the index view (e.g. ["A", "B", "C" ... "Z", "#"])
- (nullable NSArray<NSString *> *)indexTitlesForCollectionView:(UICollectionView *)collectionView API_AVAILABLE(tvos(10.2));
您可能已经阅读了 Apple 的文档,其中指出 indexTitle 方法可用于 iOS 10.3+ 和 tvOS 10.2+:https://developer.apple.com/documentation/uikit/uicollectionviewdatasource/2851455-indextitles
我尝试在 iOS 上实现它,但它不起作用,这让我相信他们在文档中不知何故犯了一个错误。
我只是通过将以下内容添加到我的数据源来获得在 iPhone 上运行的标题:
func indexTitles(for collectionView: UICollectionView) -> [String]? {
guard !RunUtilities.isIpad else { return nil }
return Array(Set(objects.map{ String([=10=].name.prefix(1)) })).sorted(by: { [=10=] < })
}
func collectionView(_ collectionView: UICollectionView, indexPathForIndexTitle title: String, at index: Int) -> IndexPath {
guard let index = objects.firstIndex(where: { [=10=].name.prefix(1) == title }) else {
return IndexPath(item: 0, section: 0)
}
return IndexPath(item: index, section: 0)
}
编辑:看起来这是从 iOS 14 开始工作的。我安装了 13.4 运行时但标题不显示。