是否可以根据 UIView 的标签对 NSArray 集合进行排序?
我见过类似的例子,
[someArray sortUsingSelector:@selector(compare:)];
我正在寻找一个存储 UIView 集合的示例,并且我想根据创建新 UIView 时以编程方式假设的标签按升序或降序对它们进行排序。
如果有人可以提供代码示例或说明,那就太好了
I've seen examples of something like
[someArray sortUsingSelector:@selector(compare:)];
I'm looking for an example that stores a collection of UIViews and I'd like to sort them in ascending or descending order based on tags I assume programatically when a new UIView is created.
If someone could provide a code example or instructions that would be fantastic
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
NSArray 提供了许多对数组进行排序的方法(所有方法都列在 NSArray 文档中的“排序”标题下)。您可以定义一个函数、方法或排序描述符来根据视图的标签进行比较以进行排序。例如,下面是使用 NSSortDescriptor 的实现:
NSArray provides a number of methods to sort an array (all listed under the "Sorting" header in the NSArray docs). You can define a function, a method or a sort descriptor to compare the views based on their tags for sorting. For example, here's an implementation using NSSortDescriptor:
假设我的顺序正确,也就是说,这种使用的方法必须位于 UIView 上(使用类别很容易完成)。还有其他几种方法可以获得相同的结果,请参阅文档 对数组进行排序。
Assuming I got the ordering right, that is, and the methods for this use would have to be on UIView (easily done with a category). There are several other ways of getting the same result, see the docs Sorting Arrays.