我有順序添加子視圖到scrollview的問題。如何順序添加子視圖到UIScrollView
我有來我從中解析爲業務對象的數組服務器返回一個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");
}
結果 - 儘管我嘗試了很多東西 - 總是一樣的:scrollView一次添加子視圖,而不是通過循環處理。我不明白這是怎麼可能的。如果我在循環結尾添加一個sleep(),它會以某種方式等待整個循環結束,然後它將子視圖顯示爲已添加。它甚至知道結果數組有多長?我在我的智慧結束,請幫助。