2012-02-07 48 views
-1

我正在製作像iOS應用程序的目錄。使用Xcode 4.2和故事板。如何從選定的UITableView跳轉到scrollView?

此應用程序有兩個視圖,每個視圖都可以通過標籤欄控制器進行更改。

應用程序圖像在這裏。 http://www.0502.me/help/xcode-cat.png

我做了目錄視圖和Table of Contents(TableView)。 但我無法將目錄視圖從目錄視圖更改。

我覺得當目錄的頁碼值被點擊時,目錄視圖的變量和視圖的變化會是很好的結果。

第一視圖是目錄視圖。 它由FirstViewController控制,並且爲了更改圖像比例,PageView類也可以使用。

第二視圖是目錄,由ListViewController控制。它解析plist(xml),並操作單元格值(標題和頁碼)。

代碼如下,請幫忙。

FirstViewController
@implementation TableOfContentSecondViewController

@synthesize currentPage; 

    - (void)didReceiveMemoryWarning 
    { 
     [super didReceiveMemoryWarning]; 
     // Release any cached data, images, etc that aren't in use. 
    } 

    #pragma mark - View lifecycle 

    -(void)pageLoad:(UIScrollView *)scrollView { 

     currentPage = scrollView.contentOffset.x/scrollView.bounds.size.width; 

     NSLog(@"CurrentPage- %i",currentPage); 
     NSLog(@"CurrentOffSet- %f",scrollView.contentOffset.x); 

     int pageWidth = self.view.frame.size.width; 
     int pageHeight = self.view.frame.size.height; 

     NSLog(@"pageWidth- %i",pageWidth); 

     prevPage.frame = CGRectMake(
            pageWidth * (currentPage - 1), 
            0, 
            pageWidth, 
            pageHeight 
            ); 

     if (currentPage > 0) { 
      [prevPage setImage:[NSString stringWithFormat:@"%d", (currentPage - 1) % kPageNum]]; 
      prevPage.hidden = NO; 
     } 
     else { 
      prevPage.hidden = YES; 
     } 

     currPage.frame = CGRectMake(
             pageWidth * currentPage, 
             0, 
             pageWidth, 
             pageHeight 
             ); 

     [currPage setImage:[NSString stringWithFormat:@"%d", currentPage % kPageNum]]; 
     currPage.hidden = NO; 

     nextPage.frame = CGRectMake(
            pageWidth * (currentPage + 1), 
            0, 
            pageWidth, 
            pageHeight 
            ); 

     if(currentPage < (kPageNum -1)) { 
      [nextPage setImage:[NSString stringWithFormat:@"%d", (currentPage + 1) % kPageNum]]; 

      nextPage.hidden = NO; 
     } 
     else { 
      nextPage.hidden = YES; 
     } 
    } 


    #pragma mark - View lifecycle 

    - (void)viewDidLoad 
    { 
     [super viewDidLoad]; 
     // Do any additional setup after loading the view, typically from a nib. 

     UIScrollView *scrollView = [[UIScrollView alloc] init]; 

     scrollView.frame = self.view.bounds; 
     scrollView.contentSize = CGSizeMake(self.view.frame.size.width * kPageNum, 
              self.view.frame.size.height 
              ); 

     //NSLog(@"ContentSize- %@",NSStringFromCGSize(scrollView.contentSize)); 

     scrollView.pagingEnabled = YES; 
     [self.view addSubview:scrollView]; 

     scrollView.delegate = self; 

     prevPage = [[PageView alloc] initWithFrame:self.view.bounds]; 
     [scrollView addSubview:prevPage]; 

     currPage = [[PageView alloc] initWithFrame:self.view.bounds]; 
     [scrollView addSubview:currPage]; 

     nextPage = [[PageView alloc] initWithFrame:self.view.bounds]; 
     [scrollView addSubview:nextPage]; 

     [self pageLoad:scrollView]; 

    } 

    -(void)scrollViewDidScroll:(UIScrollView *)scrollView { 
     CGFloat position = scrollView.contentOffset.x/scrollView.bounds.size.width; 

     CGFloat delta = position - (CGFloat)currentPage; 

     if (fabs(delta) >= 1.0f) { 
      [self pageLoad:scrollView]; 
     } 

     //NSLog(@"ContentOffset- %f", scrollView.contentOffset.x); 
    } 

    - (void)viewDidUnload 
    { 
     [super viewDidUnload]; 
     // Release any retained subviews of the main view. 
     // e.g. self.myOutlet = nil; 
    } 

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
    { 
     // Return YES for supported orientations 
     if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { 
      return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
     } else { 
      return YES; 
     } 
    } 

    @end 

PageView.m @implementation網頁瀏覽

- (id)initWithFrame:(CGRect)frame 
    { 
     self = [super initWithFrame:frame]; 
     if (self) { 
      // Initialization code 
      imageView = [[UIImageView alloc] initWithFrame:self.bounds]; 
      [self addSubview:imageView]; 

      self.delegate = self; 
      self.minimumZoomScale = 1.0; 
      self.maximumZoomScale = 4.0; 
     } 
     return self; 
    } 

    -(void)adjustScrollView:(BOOL)animate { 
     [self setZoomScale:self.minimumZoomScale animated:animate]; 
    } 

    -(void)setImage:(NSString *)image { 
     NSString *path = [[NSBundle mainBundle] pathForResource:image ofType:@"jpg"]; 

     NSLog(@"Image- %@",image); 

     /* 
     TableOfContentSecondViewController *tcController = [[TableOfContentSecondViewController alloc] init]; 
     tcController.currentPage = 120; 
     */ 

     imageView.image = [UIImage imageWithContentsOfFile:path]; 

     [self adjustScrollView:NO]; 
    } 

    -(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { 
     return imageView; 
    } 

    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
     UITouch *touch = [touches anyObject]; 

     if([touch tapCount] > 1) { 
      [self adjustScrollView:YES]; 
     } 
    } 


    /* 
    // Only override drawRect: if you perform custom drawing. 
    // An empty implementation adversely affects performance during animation. 
    - (void)drawRect:(CGRect)rect 
    { 
     // Drawing code 
    } 
    */ 

    @end 

SecondViewController爲目錄。 如果沒有子元素,則返回頁碼。

ListViewController.m

@implementation ListViewController 

    @synthesize tableDataSource,currentTitle,currentLevel; 


    - (id)initWithStyle:(UITableViewStyle)style 
    { 
     self = [super initWithStyle:style]; 
     if (self) { 
      // Custom initialization 
     } 
     return self; 
    } 

    - (void)didReceiveMemoryWarning 
    { 
     // Releases the view if it doesn't have a superview. 
     [super didReceiveMemoryWarning]; 

     // Release any cached data, images, etc that aren't in use. 
    } 

    #pragma mark - View lifecycle 

    - (void)viewDidLoad 
    { 
     [super viewDidLoad]; 

     if(currentLevel == 0) { 
      NSArray *tempArray = [[NSArray alloc] init]; 
      self.tableDataSource = tempArray; 

      TableOfContentAppDelegate *AppDelegate = (TableOfContentAppDelegate *)[[UIApplication sharedApplication] delegate]; 
      self.tableDataSource = [AppDelegate.data objectForKey:@"Rows"]; 
      self.navigationItem.title = @"Table of Contents"; 
     } 
     else 
      self.navigationItem.title = currentTitle; 

     // Uncomment the following line to preserve selection between presentations. 
     // self.clearsSelectionOnViewWillAppear = NO; 

     // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
     // self.navigationItem.rightBarButtonItem = self.editButtonItem; 
    } 

    - (void)viewDidUnload 
    { 
     [super viewDidUnload]; 
     // Release any retained subviews of the main view. 
     // e.g. self.myOutlet = nil; 
    } 

    - (void)viewWillAppear:(BOOL)animated 
    { 
     [super viewWillAppear:animated]; 
    } 

    - (void)viewDidAppear:(BOOL)animated 
    { 
     [super viewDidAppear:animated]; 
    } 

    - (void)viewWillDisappear:(BOOL)animated 
    { 
     [super viewWillDisappear:animated]; 
    } 

    - (void)viewDidDisappear:(BOOL)animated 
    { 
     [super viewDidDisappear:animated]; 
    } 

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
    { 
     // Return YES for supported orientations 
     return (interfaceOrientation == UIInterfaceOrientationPortrait); 
    } 

    #pragma mark - Table view data source 

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
    { 
    #warning Potentially incomplete method implementation. 
     // Return the number of sections. 
     return 1; 
    } 

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    { 
    #warning Incomplete method implementation. 
     // Return the number of rows in the section. 
     return [self.tableDataSource count]; 
    } 

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     static NSString *CellIdentifier = @"Cell"; 

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     } 
     NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row]; 
     cell.textLabel.text = [dictionary objectForKey:@"Title"]; 

     return cell; 

     // Configure the cell... 

     return cell; 
    } 



    #pragma mark - Table view delegate 

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    { 

     NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row]; 

     NSArray *Children = [dictionary objectForKey:@"Children"]; 

     if([Children count] == 0) { 

      /* I want to Jump to selected page on scrollView */ 

     } 
     else { 
      ListViewController *rvController = [[ListViewController alloc] initWithStyle:UITableViewStylePlain]; 

      rvController.currentLevel += 1; 

      rvController.currentTitle = [dictionary objectForKey:@"Title"]; 

      [self.navigationController pushViewController:rvController animated:YES]; 

      rvController.tableDataSource = Children; 
     } 

     // Navigation logic may go here. Create and push another view controller. 
     /* 
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil]; 
     // ... 
     // Pass the selected object to the new view controller. 
     [self.navigationController pushViewController:detailViewController animated:YES]; 
     */ 
    } 

    @end  @implementation ListViewController 

    @synthesize tableDataSource,currentTitle,currentLevel; 


    - (id)initWithStyle:(UITableViewStyle)style 
    { 
     self = [super initWithStyle:style]; 
     if (self) { 
      // Custom initialization 
     } 
     return self; 
    } 

    - (void)didReceiveMemoryWarning 
    { 
     // Releases the view if it doesn't have a superview. 
     [super didReceiveMemoryWarning]; 

     // Release any cached data, images, etc that aren't in use. 
    } 

    #pragma mark - View lifecycle 

    - (void)viewDidLoad 
    { 
     [super viewDidLoad]; 

     if(currentLevel == 0) { 
      NSArray *tempArray = [[NSArray alloc] init]; 
      self.tableDataSource = tempArray; 

      TableOfContentAppDelegate *AppDelegate = (TableOfContentAppDelegate *)[[UIApplication sharedApplication] delegate]; 
      self.tableDataSource = [AppDelegate.data objectForKey:@"Rows"]; 
      self.navigationItem.title = @"Table of Contens"; 
     } 
     else 
      self.navigationItem.title = currentTitle; 

     // Uncomment the following line to preserve selection between presentations. 
     // self.clearsSelectionOnViewWillAppear = NO; 

     // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
     // self.navigationItem.rightBarButtonItem = self.editButtonItem; 
    } 

    - (void)viewDidUnload 
    { 
     [super viewDidUnload]; 
     // Release any retained subviews of the main view. 
     // e.g. self.myOutlet = nil; 
    } 

    - (void)viewWillAppear:(BOOL)animated 
    { 
     [super viewWillAppear:animated]; 
    } 

    - (void)viewDidAppear:(BOOL)animated 
    { 
     [super viewDidAppear:animated]; 
    } 

    - (void)viewWillDisappear:(BOOL)animated 
    { 
     [super viewWillDisappear:animated]; 
    } 

    - (void)viewDidDisappear:(BOOL)animated 
    { 
     [super viewDidDisappear:animated]; 
    } 

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
    { 
     // Return YES for supported orientations 
     return (interfaceOrientation == UIInterfaceOrientationPortrait); 
    } 

    #pragma mark - Table view data source 

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
    { 
    #warning Potentially incomplete method implementation. 
     // Return the number of sections. 
     return 1; 
    } 

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    { 
    #warning Incomplete method implementation. 
     // Return the number of rows in the section. 
     return [self.tableDataSource count]; 
    } 

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     static NSString *CellIdentifier = @"Cell"; 

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     } 
     NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row]; 
     cell.textLabel.text = [dictionary objectForKey:@"Title"]; 

     return cell; 

     // Configure the cell... 

     return cell; 
    } 



    #pragma mark - Table view delegate 

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    { 

     NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row]; 

     NSArray *Children = [dictionary objectForKey:@"Children"]; 

     if([Children count] == 0) { 

      /* I want to Jump to selected page on scrollView */ 

     } 
     else { 
      ListViewController *rvController = [[ListViewController alloc] initWithStyle:UITableViewStylePlain]; 

      rvController.currentLevel += 1; 

      rvController.currentTitle = [dictionary objectForKey:@"Title"]; 

      [self.navigationController pushViewController:rvController animated:YES]; 

      rvController.tableDataSource = Children; 
     } 

     // Navigation logic may go here. Create and push another view controller. 
     /* 
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil]; 
     // ... 
     // Pass the selected object to the new view controller. 
     [self.navigationController pushViewController:detailViewController animated:YES]; 
     */ 
    } 

    @end 

回答

0
+0

謝謝您的回覆,鎮流器。 我試圖「你的第二個iOS應用程序」,但發現錯誤。 可能是故事板設置不正確。 我會重新嘗試,謝謝。 – 2012-02-09 09:10:26

相關問題