如何在UITableView中显示NSSet?

时间:2022-01-03 20:11:27

How would you proceed to display the content of an NSSet in a UITableView?

您将如何继续在UITableView中显示NSSet的内容?

As you know, the table view will ask for the element at a given row, but since the NSSet elements aren't ordered, this doesn't mix well.

如您所知,表视图将询问给定行的元素,但由于NSSet元素未被排序,因此不能很好地混合。

My current solution is to iterate through the NSSet until I reach the element at a given index, but this really doesn't feel right.

我目前的解决方案是迭代NSSet直到我到达给定索引处的元素,但这确实感觉不对。

You may ask why I don't use an NSArray instead. This is because I'm using Core Data, which uses NSSets to store collection of elements.

您可能会问为什么我不使用NSArray。这是因为我使用的是Core Data,它使用NSSets存储元素集合。

Any help would be appreciated.

任何帮助,将不胜感激。

1 个解决方案

#1


29  

Since you're only displaying the elements, why not take the NSSet, and call -[allObjects] on it to get a NSArray?

由于你只是显示元素,为什么不采用NSSet,并调用 - [allObjects]来获取NSArray?

If your data changes, you'll have to re-fetch anyway, even with a NSSet, and apart from the initial conversion from the NSSet, using the array is quite fast.

如果您的数据发生了变化,即使使用NSSet,您也必须重新获取,除了从NSSet的初始转换之外,使用该数组非常快。

#1


29  

Since you're only displaying the elements, why not take the NSSet, and call -[allObjects] on it to get a NSArray?

由于你只是显示元素,为什么不采用NSSet,并调用 - [allObjects]来获取NSArray?

If your data changes, you'll have to re-fetch anyway, even with a NSSet, and apart from the initial conversion from the NSSet, using the array is quite fast.

如果您的数据发生了变化,即使使用NSSet,您也必须重新获取,除了从NSSet的初始转换之外,使用该数组非常快。