loadNibName method is too slow - how to make it quicker?

主宰稳场 提交于 2019-12-01 20:18:04

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!