NSCollectionView 中项目的索引
在我的集合视图中,我需要为每个项目生成一个索引。当项目被重新排序时,我需要这个索引来更新它的新位置。
数据是托管 NSArrayController 中的核心数据实体。 我最接近的可能解决方案是在实体类上实现此方法,然后使用 representedObject.dynamicIndex 将其绑定到 UI。
- (NSNumber *) dynamicIndex
{
NSInteger r = [[[[self managedObjectContext] registeredObjects] allObjects] indexOfObject:self];
NSNumber *result = [NSNumber numberWithInt:r];
return result;
}
这个解决方案充其量只是粗略的,并且没有真正的功能,因为它不一定反映集合视图中的顺序。
有人有在 NSCollectionView 中生成或检索项目索引的模型/机制吗?
In my collection view I need to generate an index for each item. As Items get reordered I need this index to update with its new position.
The data are Core Data entities in a managed NSArrayController.
The closest I have come to a possible solution is implementing this method on the entity class and then using representedObject.dynamicIndex to bind it to the UI.
- (NSNumber *) dynamicIndex
{
NSInteger r = [[[[self managedObjectContext] registeredObjects] allObjects] indexOfObject:self];
NSNumber *result = [NSNumber numberWithInt:r];
return result;
}
This solution is sketchy at best, and not really functional as it doesn't necessarily reflect the order in the collection view.
Anyone have a model / mechanism for generating or retrieving item indexes in an NSCollectionView?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,确保您理解“实体”和“实例”之间的区别(并正确使用术语)。它在与他人沟通您的问题/解决方案方面发挥着重要作用。
第二:不要担心 NSCollectionViewItems ...担心每个“表示的对象”,它保存在某个容器中。
第三:您是否希望显示顺序成为实体的持久属性,或者您只需要知道该项目目前所处的位置,而不管它稍后可能是什么?重要的问题。
第四:Core Data 没有给你有序集合的概念。这是为了支持诸如 NSSQLiteStoreType 之类的存储类型,您可能只想在几个项目(或一个)中出现错误,而不加载整个列表。因此,如果您想要持久的排序顺序,您就得靠自己了。为此,只需向实体添加一个名为“sortOrder”的属性并将其设置为数字类型。
第五:由于上面的“无有序集合”问题,您尝试从一个由集合构建的数组中查找给定实体实例的索引,该数组因不确定的顺序而出错,注定会失败。
第六:由于您使用的是数组控制器,因此您需要设置其排序描述符。您需要使用“sortOrder”键。这样,您获取的实例将始终按其“sortOrder”排序。
第七也是最后:如果您试图获取数组控制器对象集/数组中任何对象的索引,您将需要向它询问其 -arrangedObjects,因此您将得到数组控制器控制的已排序集合中对象的索引。
希望有帮助。
Lion (10.7) 更新
关于我的第六点:如果您在应用程序中定位 10.7 及更高版本,[NSManagedObject 现在为您提供有序关系。][1 ] 使用 -mutableOrderedSetValueForKey: 和 -mutableOrderedSetValueForKey: 设置和检索 NSOrderedSets。耶!
First, make sure you understand the difference between (and properly use the terminology of) "entity" and "instance." It makes all the difference in communicating your problems/solutions with others.
Second: Don't worry about NSCollectionViewItems ... worry about each one's "represented object," which is held in some container.
Third: Did you want the display order to be a persistent attribute of your entity or do you just need to know what position the item is in at the moment, regardless of what it might be later? Important question.
Fourth: Core Data does not give you the concept of ordered collections. This is to support store types such as NSSQLiteStoreType, where you might only want to fault in a few items (or one) without loading the whole list. Therefore, you're on your own if you want a persistent sort order. To do this, just add an attribute to your entity called "sortOrder" and make it a number type.
Fifth: Because of the "no ordered collections" issue above, your attempt to find the index of a given instance of your entity from an array, built from a set, which was faulted in with a nondeterministic order is doomed to failure.
Sixth: Since you're using an array controller, you'll need to set its sort descriptors. You'll want to use your "sortOrder" key. That way, your fetched instances will always be kept sorted by their "sortOrder."
Seventh and finally: If you're trying to get the index of any objects in your array controller's set/array of objects, you'll want to ask it for its -arrangedObjects, so you're getting the index of the object in the sorted collection the array controller controls.
Hope that helps.
Update for Lion (10.7)
With regard to my sixth point: If you're targeting 10.7 and above in your application, [NSManagedObject now gives you ordered relationships.][1] Use -mutableOrderedSetValueForKey: and -mutableOrderedSetValueForKey: to set and retrieve NSOrderedSets. Yay!