2011-06-30 101 views
0

我有一個包含webview和webview的滾動視圖顯示來自xml的內容。例如我有10頁。所以如果一個想要去特定頁面我應該怎麼去那個頁面UIScrollView跳轉特定頁面

int j = myindex;

int k=0; 
for (int i=0; i < [appDelegate.books count]; i++) { 

    if (j>i) { 

     // //j++; 

     continue; 

    } 


    CGRect frame; 
    frame.origin.x = self.scrollView.frame.size.width * k; 
    frame.origin.y = 0; 
    frame.size = self.scrollView.frame.size; 
    UIView*subView=[[UIView alloc ]initWithFrame:frame]; 
    //subView.backgroundColor=[appDelegate.books objectAtIndex:i]; 
    [self.scrollView addSubview:subView]; 



    CGRect myframe=CGRectMake(8, 16, 371, 242); 
    UIImageView*imageView=[[UIImageView alloc] initWithFrame:myframe]; 
    Book*aBook=[appDelegate.books objectAtIndex:i]; 

    NSString*myimage=aBook.mainimgurl; 
    myimage = [myimage stringByReplacingOccurrencesOfString:@"\n" withString:@""]; 
    myimage = [myimage stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; 
    UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:myimage]]]; 


    j++; 
    imageView.image=image; 

    CGRect mywebframe=CGRectMake(0, 30, 768, 876); 

    UIWebView*webView=[[UIWebView alloc] initWithFrame:mywebframe]; 

    /*NSString*title=aBook.title; 

    NSString*[email protected]"Rask og grønn"; 
    title = [title stringByReplacingOccurrencesOfString:@"\n" withString:@""]; 
    title = [title stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; 

    NSString*[email protected]"Fortidsklimaet på Svalbard"; 

    if ([title isEqualToString:mytitle ] || [title isEqualToString:mytitle1]) { 

     htmlString=aBook.magazinenumberdescription; 


    }*/ 


    if (aBook.fulltext==nil) { 

     htmlString=aBook.magazinenumberdescription; 

    } 



    else { 

    htmlString=aBook.fulltext; 


} 


      //NSString*htmlString=aBook.description; 


    [webView loadHTMLString:htmlString baseURL:nil]; 
    [subView addSubview:webView];  

    CGRect myframe1=CGRectMake(387, 16, 363, 292); 
    UITextView*textView=[[UITextView alloc] initWithFrame:myframe1]; 
    textView.font=[UIFont fontWithName:@"Times New Roman" size:16];    
    NSString*description=aBook.description; 

    description = [description stringByReplacingOccurrencesOfString:@"\n" withString:@""]; 
    description = [description stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; 

    description = [description stringByReplacingOccurrencesOfString:@"&aring;" withString:@"å"]; 
    description = [description stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; 
    description = [description stringByReplacingOccurrencesOfString:@"&oslash;" withString:@"ø"]; 
    description = [description stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; 


    description = [description stringByReplacingOccurrencesOfString:@"&aelig;" withString:@"æ"]; 
    description = [description stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; 


    description = [description stringByReplacingOccurrencesOfString:@"&AElig;" withString:@"Æ"]; 
    description = [description stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; 


    description = [description stringByReplacingOccurrencesOfString:@"&Oslash;" withString:@"Ø"]; 
    description = [description stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; 


    description = [description stringByReplacingOccurrencesOfString:@"&Aring" withString:@"Å"]; 
    description = [description stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; 

    k++; 

    textView.text=description; 
    // [subView addSubview:textView]; 

    // [subView addSubview:imageView];  



}  



//NSInteger numberofPages=10-j; 
NSInteger numberofPages=[appDelegate.books count]; 



self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * numberofPages, self.scrollView.frame.size.height); 
[pageControl setNumberOfPages:numberofPages]; 
[pageControl setActivePageColor:[UIColor clearColor]]; 
[pageControl setInactivePageColor:[UIColor clearColor]]; 
+0

你有許多Web視圖中顯示爲滾動視圖頁面? – 2011-06-30 05:26:37

+0

是的,我在循環中創建webview並將該webview添加到滾動視圖 – Ali

+0

是否意味着您有10個Web視圖?或者一個Web視圖包含一個長頁面? – Maulik

回答

14

試試這個:

- (void) viewDidLoad // Or any other method 
{ 
[myScroll scrollRectToVisible:CGRectMake(320*pageNumber, 0, 320 , 240) animated:NO]; 
//320 is a width of my page,you have to replace it with your page's width 
[self.view addSubview:myScroll];   
} 

希望這可以幫助你..

+0

的重複並在您的scrollView中啓用最佳分頁。 – Ved

+0

@Ved:yead ...我沒有提到.. – Maulik

+0

@Maulik:我發佈的代碼顯示的內容正確的問題是,當你在加載第3頁直接你可以滾動到4 5至10,但是不是前一個2 1 – Ali