Question

I create an NSCollectionView using cocoa binding.I find that when the NSMutableArray ,which is set as the NSCollectionView's content , finish to sort,the NSCollectionView has no change. Here is my sort code:

-(void)updateOrder
{

    [self willChangeValueForKey:@"bookmarksArray"];
    [_bookmarksArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {

    /**
     Here is the sort code.
     **/

    }];
    [self didChangeValueForKey:@"bookmarksArray"];
}

I can catch the removed change and inserted change,but I can't catch the reorder change.Why?

Was it helpful?

Solution

This code will not actually change the order. sortedArrayUsingComparator: returns a whole new array. It doesn't sort an existing array in place. The sorting operation is effectively thrown away -- _bookmarksArray is never changed. You could, for instance, change it to:

-(void)updateOrder
{

    [self willChangeValueForKey:@"bookmarksArray"];
    _bookmarksArray = [_bookmarksArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {

    /**
     Here is the sort code.
     **/

    }];
    [self didChangeValueForKey:@"bookmarksArray"];
}

(Assuming ARC here. Same idea but with appropriate retain/release calls if not ARC.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top