2012-07-09 79 views
3

我試圖實現分頁與本教程2次:的UIScrollView分頁與2次

Multiple virtual pages in a UIScrollView with just 2 child views

我想通過把UIScrollView的相關網頁以修改本,並在每個頁面有新的內容更新它:

-(void)addContent:(NSString*)txt{ 

    int size = 5; 
    UITextView *txtView; 
    int row = 0; 

    //from left 
    int xPlace = 0; 
    //space between row's 
    int rowSpace = 20; 
    //from top 
    int yPlace = 10; 

    // int button_with = scrollView.frame.size.width; 
    int button_height = 20; 

    for (int i = 0; i < size; i++) { 


     txtView = [[UITextView alloc] initWithFrame:CGRectMake(xPlace, row*rowSpace+yPlace, scrollView.frame.size.width, 2*button_height)]; 
     [txtView setFont:[UIFont fontWithName:@"ArialMT" size:14]]; 
     txtView.backgroundColor = [UIColor blueColor]; 

     [txtView setText:[NSString stringWithFormat:@"Numer %@ - %i",txt,i]]; 

     // answerCheck.titleLabel.font = [UIFont boldSystemFontOfSize:fontSize]; 


     [scrollView addSubview:txtView]; 
     [txtView release]; 
     row++; 
    } 

    [scrollView setContentSize:CGSizeMake(scrollView.frame.size.width, txtView.frame.size.height*size)]; 
} 

這個想法是當滾動只是用新的內容替換uiviewcontroller。它調用:

- (void)setPageIndex:(NSInteger)newPageIndex 
{ 
    pageIndex = newPageIndex; 

    if (pageIndex >= 0 && pageIndex < [[DataSource sharedDataSource] numDataPages]) 
    { 
     NSDictionary *pageData = 
      [[DataSource sharedDataSource] dataForPage:pageIndex]; 
     label.text = @"Tekstas";//[pageData objectForKey:@"pageName"]; 
     textView.text = [pageData objectForKey:@"pageText"]; 
     [self addContent:[pageData objectForKey:@"pageText"]]; 
     CGRect absoluteRect = [self.view.window 
      convertRect:textView.bounds 
      fromView:textView]; 
     if (!self.view.window || 
      !CGRectIntersectsRect(
       CGRectInset(absoluteRect, TEXT_VIEW_PADDING, TEXT_VIEW_PADDING), 
       [self.view.window bounds])) 
     { 
      textViewNeedsUpdate = YES; 
     } 
    } 
} 

在這裏,我卡住了。我的文本視圖正在更新,但UIscrollView的新內容不會在iPad模擬器中更新。

是的我的意思是在iPhone它的作品,而在iPad它不。如果我想實現這個功能,我可以改進什麼?

也許有其他的方法可以從iOS 5進行分頁嗎?

我會把源碼,試圖使工作。

Download source

回答

2

您可以只使用一個nib文件中添加UIScrollView與修復widthheight。現在將一些頁面作爲UIView並將它們添加到UIScrollView,其大小和origin相同。在主UIView

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, scrollView.frame.size.height * 2); 
} 

-(void)viewWillAppear:(BOOL)animated{ 
    CGFloat h = scrollView.frame.size.height; 
    [pageView1 setFrame:CGRectMake(0, 0, pageView1.frame.size.width, pageView1.frame.size.height)]; 

    [pageView2 setFrame:CGRectMake(0, h, pageView2.frame.size.width, pageView2.frame.size.height)]; 
} 

而且不要忘記在nib文件,檢查Paging EnabledUIScrollViewAttribute秒。

我希望對你有用!

+0

對不起,我假定你的滾動是水平的! – 2012-07-09 08:34:59

+1

我編輯你的答案,以適應他的問題 – nfarshchi 2012-07-09 10:53:39