Iphone ScrollView添加子视图:非常慢

Iphone ScrollView添加子视图:非常慢,iphone,xcode,uiscrollview,Iphone,Xcode,Uiscrollview,我正在使用scrollview显示ViewController视图。如果用户到达缓存视图的末尾,我的方法将重新加载新视图。我的NSLogs显示该方法已完成,但显示视图还需要5秒钟。 我认为[scrollView addSubview:vc.view]速度非常慢,但我没有发现任何改进 整个方法在-(void)ScrollViewDiEndDecelling:(UIScrollView*)scrollView中调用 scrollView.userInteractionEnabled = NO;

我正在使用scrollview显示ViewController视图。如果用户到达缓存视图的末尾,我的方法将重新加载新视图。我的NSLogs显示该方法已完成,但显示视图还需要5秒钟。 我认为[scrollView addSubview:vc.view]速度非常慢,但我没有发现任何改进

整个方法在-(void)ScrollViewDiEndDecelling:(UIScrollView*)scrollView中调用

 scrollView.userInteractionEnabled = NO;

    UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [activityIndicator setFrame:CGRectMake((320.f*index)+135.f, 151, 50, 50)];
    [activityIndicator setHidesWhenStopped:YES];
    [activityIndicator startAnimating];

    [scrollView addSubview:activityIndicator];

    MBBAppDelegate *delegate = (MBBAppDelegate *)[UIApplication sharedApplication].delegate;

    [delegate fetchDealsWithFilter:dealFilter fromIndex:index toIndex:(index+3) onSuccess:^(id object){

        MBBDealList *list = object;

        int i = 0;

        ProductDetailViewController *vc;

        for (MBBDeal *deal in [list deals]) {

            NSLog(@"start %i",i);
            int indexInArray = i;//[list.deals indexOfObject:deal];

            if (indexInArray+index >= numDeals) {
                return;
            }

            vc = [[ProductDetailViewController alloc] init];

            //fetch the deal and insert
            vc.numberOfDeals = numDeals;
            vc.dealIndex = index+1+indexInArray;
            vc.dealFilter = dealFilter;
            vc.deal = deal;

            vc.view.frame = CGRectMake(320.f*(index+indexInArray), 0.f, 320.f, 436.f);

            [scrollView addSubview:vc.view];

            [productDetailViewControllers insertObject:vc atIndex:(index+indexInArray)];

            i++;
        }
        [activityIndicator stopAnimating];
        scrollView.userInteractionEnabled = YES;

    }];

}

有人知道我可以如何改进我的方法吗?

从您的问题中我可以理解的是,您用来显示数据获取过程的活动指示器没有正确使用。 即使在活动指示器消失后,数据提取过程仍在工作

为此,您可以做两件事:
以太使用performSelectorInBackground方法在后台调用数据提取方法,或将活动指示器放置在创建数据提取方法的应用程序代理中。

使用工具分析应用程序时,在这5秒钟内,你发现大部分时间都花在哪里?我应该在乐器中使用哪种预设?我发现没有任何帮助。此延迟是因为数据提取过程……数据提取完成后,您可以在后台线程中编写数据提取过程更新UI,或在ViewDidAppear方法中编写数据提取过程我编辑了我的方法,并使用performSelectorInBackground重新加载,重新加载需要半秒钟(NSLog说)但是延迟一段时间后视图仍然显示。可能addSubview:方法不是最快的方法?我有一个包含scrollview的ViewController,重载方法位于其中,所以在appdelegate中执行ActivityIndicator不是一个选项?!我做了另一次测量,一次用了7秒、8秒,另一次用了17秒秒,但我找不到原因。当您在“ViewDidLoad”函数中写入数据获取方法时,视图会延迟。如果您在“ViewDidDisplay”函数中获取数据,则可以更快地加载视图