2012-02-07 6 views
0

私はscrollViewを含むContentViewControllerを持っています。特定のインデックスに2番目のUIScrollViewをロードし、結果を表示

scrollViewは、3ページ(1ページに9アイテム)にわたって27の選択可能なアイテムを表示します。これは正常に動作します。

ユーザーが項目のいずれかをタップすると、27ページにわたる27項目のすべてを含むようにスクロールビューの配列が再構築されます。ページごとに1項目(別の表示コントローラーを使用して表示)。したがって、アイテムを選択すると詳細ビューが表示されます。

ただし、ユーザーが選択した項目に関係なく、2番目のスクロールビューには空白が読み込まれます。ページコントロールによれば、スクロールビューは適切な場所にありますが、何も表示されません。アイテムが表示されるが、インデックスをリストの先頭にリセットするビューをスクロールするまではありません。

恐ろしいほどの量のコードについては申し訳ありませんが、これは間違っていることを示すのに役立ちます。

アドバイスをいただきありがとうございます。あなたが逃した

- (void)objectSelected: (SelectableObject *)objectSelected { 

detailView = TRUE; 
self.selectedObject = objectSelected; 

[self drawScrollingView]; 

}

- (void)drawScrollingView { 

//Clear out any previous views 
[scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; 

//Set the background and other interface elements 
if (detailView) { 
    self.background.image = [UIImage imageNamed:@"SelectB.png"]; 
    self.nameField.hidden = FALSE; 
    pageControl.frame = CGRectMake(0, 380, pageControl.frame.size.width, pageControl.frame.size.height); 
    numberPages = [self.contentList count]; 
} 
else { 
    self.background.image = [UIImage imageNamed:@"SelectA1.png"]; 
    pageControl.backgroundColor = [UIColor orangeColor]; 
    //Determine the number of pages 
    int items = [self.contentList count]; 
    numberPages = items/9; 
    if (items % 9 != 0) 
     numberPages++; 
} 

// view controllers are created lazily 
// in the meantime, load the array with placeholders which will be replaced on demand 
NSMutableArray *controllers = [[NSMutableArray alloc] init]; 
for (unsigned i = 0; i < numberPages; i++) { 
    [controllers addObject:[NSNull null]]; 
} 
self.viewControllers = controllers; 
[controllers release]; 

// a page is the width of the scroll view 
scrollView.pagingEnabled = YES; 
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * numberPages, scrollView.frame.size.height); 
scrollView.showsHorizontalScrollIndicator = NO; 
scrollView.showsVerticalScrollIndicator = NO; 
scrollView.scrollsToTop = NO; 
scrollView.delegate = self; 

pageControl.numberOfPages = numberPages; 
pageControl.hidesForSinglePage = TRUE; 

if (!detailView) 
    pageControl.currentPage = 0; 
else 
    pageControl.currentPage = self.selectedObject.number; 

// pages are created on demand 
// load the visible page 
// load the page on either side to avoid flashes when the user starts scrolling 
[self loadScrollViewWithPage:pageControl.currentPage-1]; 
[self loadScrollViewWithPage:pageControl.currentPage]; 
[self loadScrollViewWithPage:pageControl.currentPage+1]; 

}

- (void)loadScrollViewWithPage:(int)page { 
if (page < 0) 
    return; 
if (page >= numberPages) 
    return; 

NSLog(@"PAGE: %i", page); 

if (!detailView) { 
    //Get the subset of objects, depending on the page, and pass it to the controller 
    //There are 9 objects per page, so page 0 covers 0-8, page 1 covers 9-17, 2 18-26. 
    //However, there may not be a total of 9 
    int startIndex = page*9; 
    int length = 9; 
    int total = [self.contentList count]; 
    if (startIndex+length > total) 
     length = total-startIndex; 

    NSRange theRange; 
    theRange.location = startIndex; 
    theRange.length = length; 
    NSArray *selectionSubset = [self.contentList subarrayWithRange:theRange]; 

    // replace the placeholder if necessary 
    SelectionScreenViewController *controller = [viewControllers objectAtIndex:page]; 
    if ((NSNull *)controller == [NSNull null]) { 
     controller = [[SelectionScreenViewController alloc] initWithSet:selectionSubset]; 
     [controller setDelegate:self]; 
     [viewControllers replaceObjectAtIndex:page withObject:controller]; 
     [controller release]; 
    } 
    // add the controller's view to the scroll view 
    if (controller.view.superview == nil) { 
     CGRect frame = scrollView.frame; 
     frame.origin.x = frame.size.width * page; 
     frame.origin.y = 0; 
     controller.view.frame = frame; 
     [scrollView addSubview:controller.view]; 
    } 
} 
else { 
    NSLog(@"DETAIL VIEW"); 
    [self.navigationController setToolbarHidden:NO animated:YES]; 

    // replace the placeholder if necessary 
    SelectionDetailViewController *controller = [viewControllers objectAtIndex:page]; 

    if ((NSNull *)controller == [NSNull null]) { 
     NSLog(@"controller is NULL"); 
     controller = [[SelectionDetailViewController alloc] initWithObject:[self.contentList objectAtIndex:page]]; 
     [controller setDelegate:self]; 
     [viewControllers replaceObjectAtIndex:page withObject:controller]; 
     [controller release]; 
    } 
    // add the controller's view to the scroll view 
    if (controller.view.superview == nil) { 
     NSLog(@"controller superview is NIL"); 
     CGRect frame = scrollView.frame; 
     frame.origin.x = frame.size.width * page; 
     frame.origin.y = 0; 
     controller.view.frame = frame; 
     [scrollView addSubview:controller.view]; 
    } 

}

- (void)scrollViewDidScroll:(UIScrollView *)sender { 
// We don't want a "feedback loop" between the UIPageControl and the scroll delegate in 
// which a scroll event generated from the user hitting the page control triggers updates from 
// the delegate method. We use a boolean to disable the delegate logic when the page control is used. 
if (pageControlUsed) { 
    // do nothing - the scroll was initiated from the page control, not the user dragging 
    return; 
} 

// Switch the indicator when more than 50% of the previous/next page is visible 
CGFloat pageWidth = scrollView.frame.size.width; 
NSLog(@"contentOffset: %g", scrollView.contentOffset); 

int page = floor((scrollView.contentOffset.x - pageWidth/2)/pageWidth) + 1; 
if (detailView) { //some other formula here to determine page??? 

} 


pageControl.currentPage = page; 

// Set the name 
self.selectedObject = [self.contentList objectAtIndex:page]; 
self.nameField.text = self.selectedObject.name; 

NSLog(@"NAME: %@", [[self.contentList objectAtIndex:page] name]); 
NSLog(@"ScrollViewDidScrool page: %i", page); 

// load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling) 
[self loadScrollViewWithPage:page - 1]; 
[self loadScrollViewWithPage:page]; 
[self loadScrollViewWithPage:page + 1]; 

}

+0

長すぎる質問 –

答えて

3

contentOffsetをスクロールビューに設定します。

スクロールビューの作成時に、次の行を追加します。

scrollView.contentOffset = CGPointMake(scrollView.frame.size.width * self.selectedObject.number, scrollView.frame.origin.y); 
+0

うわー、夢のように働いた。私はcontentOffsetで何かをする必要があると思ったが、それを理解できなかった。ありがとうございました!!! – Smikey

+0

ありがとうございました。 –

関連する問題