0

我正在使用滚动视图来显示视图控制器视图。如果用户到达缓存视图的末尾,我的方法会重新加载新视图。我的 NSLogs 说该方法已完成,但显示视图需要额外的 5 秒。我认为 [scrollView addSubview:vc.view] 非常慢,但我没有发现可以改进它。

整个方法在 -(void)scrollViewDidEndDecelerating:(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;

    }];

}

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

4

1 回答 1

0

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

You can do 2 things for that: ether call the data fetch method in background using performSelectorInBackground method or place the activity indicator in the app delegate where you have created your data fetch method.

于 2012-06-20T08:03:12.723 回答