在NSMutableArray中对视图进行排序

时间:2011-10-01 11:34:58

标签: objective-c random nsmutablearray sorting

我使用NSMutableArray获取特定superView的所有子视图,一切正常,但日志说:

superView contains (
"<UIView: 0x68795c0; frame = (20 172; 238 115); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0x689aad0>>",
"<UIView: 0x6e67d90; frame = (143 295; 115 115); autoresize = W+H; layer = <CALayer: 0x6e67dc0>>",
"<UIView: 0x6e6f1a0; frame = (20 49; 115 115); autoresize = W+H; layer = <CALayer: 0x6e6e830>>",
"<UIView: 0x6e6fac0; frame = (143 49; 115 115); autoresize = W+H; layer = <CALayer: 0x6e6faf0>>"
)

正如您所看到的,看起来视图会随机加载到数组中,但对我来说,它们按顺序加载很重要,因此它会从超级视图的顶部到底部加载视图。所以它应该看起来像这样:

superView contains (
"<UIView: 0x6e6f1a0; frame = (20 49; 115 115); autoresize = W+H; layer = <CALayer: 0x6e6e830>>",
"<UIView: 0x6e6fac0; frame = (143 49; 115 115); autoresize = W+H; layer = <CALayer: 0x6e6faf0>>"
"<UIView: 0x68795c0; frame = (20 172; 238 115); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0x689aad0>>",
"<UIView: 0x6e67d90; frame = (143 295; 115 115); autoresize = W+H; layer = <CALayer: 0x6e67dc0>>",
)

有人可以就如何做到这一点给我建议吗?

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

NSArray有多种排序方法;它们通常涉及一个附加的函数或方法,它将一个对象作为参数并返回NSComparisonResult的类型。来自文档:

  

一个选择器,用于标识用于比较两个元素的方法   一次。如果是,该方法应返回NSOrderedAscending   接收数组小于参数NSOrderedDescending if   接收数组大于参数,NSOrderedSame if   他们是平等的。

指定实际的比较参数。