하위 뷰를 순차적으로 스크롤 뷰에 추가하는 데 문제가 있습니다.순차적으로 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()을 추가하면, 서브 뷰를 추가하기 전에 전체 루프가 끝나기를 기다린다. 결과 배열의 길이를 어떻게 알 수 있습니까? 나는 지혜로웠다. 도와주세요.