【问题标题】:sort NSArray of UIImagerview?排序 UIImagerview 的 NSArray?
【发布时间】:2011-03-23 13:41:39
【问题描述】:

我想根据框架对 UIImageView 的 NSArray 进行排序?

我尝试使用 NSSortDescriptor,但是当我写 key=@"frame" 时出现无法识别的选择器错误。 请告诉我如何对我的数组进行排序。

提前致谢。

【问题讨论】:

    标签: iphone objective-c sorting uiimageview nsarray


    【解决方案1】:

    在你的项目中试试这个代码

    NSArray * myArray =  @[
    
                 [[UIImageView alloc] initWithFrame:CGRectMake(0, 50, 200, 100)],
                  [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 200, 100)],
                  [[UIImageView alloc] initWithFrame:CGRectMake(50, 40, 50, 100)],
                  [[UIImageView alloc] initWithFrame:CGRectMake(30, 30, 200, 100)],
                  [[UIImageView alloc] initWithFrame:CGRectMake(10, 50, 200, 67)]
    
        ];
    
    
        myArray = [myArray sortedArrayUsingComparator:^NSComparisonResult(UIImageView * view1, UIImageView * view2) {
    
            if (! CGRectIntersectsRect(view1.frame, view2.frame)) {
                return NSOrderedAscending;
            } else {
                return NSOrderedDescending;
            }
    
        }];
    

    【讨论】:

      【解决方案2】:

      您始终可以编写自己的比较器函数并使用 NSArray sortedArrayUsingSelector method 定义自定义排序行为

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-15
        • 2012-01-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多