[英]How to add indexTitles in a collection view? (iOS 10+)
As the question, I'm trying to add an "index titles" in a collection view for a fast navigation of the contents of my UIColletionView
.作为问题,我试图在集合视图中添加一个“索引标题”,以便快速导航我的
UIColletionView
的内容。
I've read that, starting from iOS 10, method like indexTitles(for:)
and collectionView(_:indexPathForIndexTitle:at:)
can help me in that as in the UITableViews .我读过,从 iOS 10 开始,像
indexTitles(for:)
和collectionView(_:indexPathForIndexTitle:at:)
可以帮助我解决UITableViews 中的问题。 But I'm not getting any result!但我没有得到任何结果! Have you figured out how to use those methods?
你知道如何使用这些方法吗? Thanks in advance for any help.
在此先感谢您的帮助。
If you look at UICollectionView.h
, indexTitlesForCollectionView
is only available in tvos:如果您查看
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));
You might have read Apple's documentation, which states that indexTitle methods are available for iOS 10.3+ and tvOS 10.2+: https://developer.apple.com/documentation/uikit/uicollectionviewdatasource/2851455-indextitles您可能已经阅读过 Apple 的文档,其中指出 indexTitle 方法可用于 iOS 10.3+ 和 tvOS 10.2+: https : //developer.apple.com/documentation/uikit/uicollectionviewdatasource/2851455-indextitles
I tried implementing it on iOS and it just doesn't work, which leads me to believe that somehow they made a mistake in the documentation.我尝试在 iOS 上实现它,但它不起作用,这让我相信他们在文档中以某种方式犯了错误。
I got the titles working on iPhones only by adding the following to my data source:我只有通过将以下内容添加到我的数据源中才能获得适用于 iPhone 的标题:
func indexTitles(for collectionView: UICollectionView) -> [String]? {
guard !RunUtilities.isIpad else { return nil }
return Array(Set(objects.map{ String($0.name.prefix(1)) })).sorted(by: { $0 < $1 })
}
func collectionView(_ collectionView: UICollectionView, indexPathForIndexTitle title: String, at index: Int) -> IndexPath {
guard let index = objects.firstIndex(where: { $0.name.prefix(1) == title }) else {
return IndexPath(item: 0, section: 0)
}
return IndexPath(item: index, section: 0)
}
EDIT: It looks like this is working from iOS 14 onwards.编辑:看起来这是从 iOS 14 开始的。 I installed the 13.4 runtime and the titles don't show.
我安装了 13.4 运行时,但没有显示标题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.