问题
I have a scroll view, which contains about 40-50 objects of different types. The object's types are defined in function of the object's location (for ex. if is the 5th object in the scroll view-> is's Object1, if it is the 11th object in the scroll view -> it's Object2 type etc.). With a for I am verifying each element of an array, and then putting them into the scroll view, with this method:
for (int i = 0; i < [myArray count]; i++){
if (i < 10){
NSArray *xib = [[NSBundle mainBundle] loadNibNamed:@"Class1" owner:nil options:nil];
for (NSObject *obj in xib){
if ([obj isKindOfClass:[Class1 class]]){
classObject = (Class1 *)obj;
break;
}
}
} else if (i > 10 && i < 20){
NSArray *xib = [[NSBundle mainBundle] loadNibNamed:@"Class2" owner:nil options:nil];
for (NSObject *obj in xib){
if ([obj isKindOfClass:[Class2 class]]){
classObject = (Class2 *)obj;
break;
}
}
}
[scrollview addSubview:classObject];
}
My problem is, that it loads very slowly. What can I do to make it quicker?
回答1:
If you are programming for IOS4+, you can use the UINib class instead. It will load a cached objects and create a copy each time needed. See this blog post.
来源:https://stackoverflow.com/questions/8123311/loadnibname-method-is-too-slow-how-to-make-it-quicker