【发布时间】:2012-01-12 19:46:55
【问题描述】:
我在将子视图按顺序添加到滚动视图时遇到问题。
我从服务器返回了一个 JSON 响应,我将其解析为一个业务对象数组,然后发送到函数 updateCarousel,如下所示:
-(void) updateCarousel: (NSArray *)response{
if(response && response.count>0){
int i=0;
self.scrollView.hidden=NO;
[self.scrollView setNeedsDisplay];
self.pageControl.hidden=NO;
[self.scrollView setContentOffset:CGPointMake(0, 0) animated:NO];
for (Business *business in response){
if (i >= MAX_INITAL_SEARCH_RESULTS)
break;
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = scrollView.frame.size;
CardView *cardView = [[CardView alloc] initWithBusinessData:business andFrame:frame];
//I've tried the following code with and without wrapping it in a GCD queue
dispatch_queue_t addingQueue = dispatch_queue_create("adding subview queue", NULL);
dispatch_async(addingQueue, ^{
[self.scrollView addSubview:cardView];
});
dispatch_release(addingQueue);
cardView.backgroundColor = [UIColor colorWithWhite:1 alpha:0];
i++;
self.scrollView.contentSize = CGSizeMake(i*(self.scrollView.frame.size.width), self.scrollView.frame.size.height);
self.pageControl.numberOfPages=i;
}
}else{
self.scrollView.hidden=YES;
self.pageControl.hidden=YES;
NSLog(@"call to api returned a result set of size 0");
}
结果——尽管我尝试了很多东西——总是一样的:滚动视图一次添加所有子视图,而不是通过循环处理它们。我不明白这怎么可能。如果我在循环末尾添加一个 sleep() ,它会以某种方式等待整个循环结束,然后再显示添加的子视图。它怎么知道结果数组有多长?我无能为力,请帮忙。
【问题讨论】:
标签: ios asynchronous uiscrollview addsubview