【发布时间】:2012-06-20 07:03:57
【问题描述】:
我正在使用滚动视图来显示视图控制器视图。如果用户到达缓存视图的末尾,我的方法会重新加载新视图。我的 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;
}];
}
有人知道我可以如何改进我的方法吗?
【问题讨论】:
-
当您使用 Instruments 分析应用程序时,您发现这 5 秒内大部分时间都花在了哪里?
-
我应该在 Instruments 中使用哪个预设?我发现没有任何帮助。
-
这个延迟是因为数据获取过程......
-
您可以在数据获取完成后在后台线程中编写数据获取过程更新您的 UI,或者在 ViewDidAppear 方法中编写数据获取过程
标签: iphone xcode uiscrollview