I have created a UIView subclass in whose drawRect method I am drawing UIImage based on the contents of an NSDictionary. My problem is that sometimes the order of the image(s) changes and one image appears over another image which is previously being drawn below the first image. My d开发者_开发问答rawRect code is as given below
NSArray* arr = [myDictionary allValues];
for (MyObject* obj in arr) {
NSString* imageName = obj.imageName;
CGRect imageRect = CGRectZero;
imageRect.origin = obj.origin;
imageRect.size = obj.size;
UIImage* img = [ UIImage imageWithContentsOfFile:imageName];
[img drawInRect:imageRect];
}
The NSDictionary content is not being sorted at all. Whenever the NSDictionary is updated in the UIView subclass then i simply calls [self setNeedsDisplay] to refresh the view. But I am not able to get why the ordering of UIImage changes sometimes.
Thanks Arnieterm
Using NSSortDescriptor along with an additional field of type NSDate in the custom object resolved the problem. Basically the keys of NSDictionary are getting re-arranged since they are of kind "0000", "0101", "4222" etc. The above solution enables keeping the order in which the contents are added to NSDictionary with no re-arrangement whatsover the values of the keys are. Thanks Arnieterm
精彩评论