简体   繁体   English

如何在集合视图中添加 indexTitles? (iOS 10+)

[英]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.

相关问题 如何确定iPad是否可以拨打电话iOS 10+ - How to determine if an iPad can place a phone call iOS 10+ 如何在 iOS 10+ 中自定义通知的简短预览 - How to customize short preview of notifications in iOS 10+ 有关在iOS 10+上实施小组SMS的建议? - Suggestions on group SMS implementation on iOS 10+? 为iOS 10+注册通知-未收到通知 - Registering for notifications for iOS 10+ - notifications not received iOS 丰富通知多个附件(iOS 10+) - iOS Rich Notifications multiple attachments (iOS 10+) iOS 如何将新项目添加到集合视图 - iOS How do I add a new item to a collection view 如何将图像从ios中的本地库添加到集合视图单元格中? - How to add image into collection view cell from local library in ios? 当我在不使用任何迁移的情况下向数据模型添加/修改新属性时,应用程序不会崩溃(IOS 10+) - App not crashing when i add/Modify new attribute to the data model without using any Migration (IOS 10+) 故事板上的iOS文本验证可支持10多个文本字段 - iOS text validation in story board for 10+ textfields 在iOS 10以上版本中,有什么方法可以可靠地唤醒应用程序 - In iOS 10+, is there ANY way to RELIABLY wake up an app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM