スクロールビューにサブビューを順番に追加する際に問題があります。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()を追加すると、ループ全体が終了してからサブビューが追加されるまで待っています。結果の配列の長さはどのように知ることさえできますか?私は私の知恵の末尾にいる、助けてください。