如何从选定的UITableView页面跳转到scrollView?

时间:2012-02-07 07:42:46

标签: objective-c xcode scrollview tableview

我正在制作像iOS应用程序一样的目录。使用Xcode 4.2和故事板。

此应用程序有两个视图,每个视图都可以通过标签栏控制器进行更改。

应用程序图像在这里。 http://www.0502.me/help/xcode-cat.png

我制作了目录视图和目录(TableView)。 但我无法将视图从目录视图更改为目录视图。

我认为在单击目录的页码值时,目录视图的变量和视图更改将是很好的结果。

First View是目录视图。 它由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 PageView

    - (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

1 个答案:

答案 0 :(得分:0)

也许这会有所帮助:

Coordinating Efforts Between View Controllers