Navigation Controller Pointer for UITableView

Discussion in 'iOS Programming' started by computerartist, Oct 3, 2010.

  1. computerartist macrumors newbie

    Joined:
    Sep 4, 2010
    #1
    What I am trying to do is to make the Application Delegate Navigation Controller the Navigation Controller for a programmatically inserted tableView (only the view, not the view with navigation controller... if this is possible.



    What I was hoping for is that I could do something like the following:


    Code:
    navController = tableViewAppDelegate.navigationController;
    


    Code:
    	tableViewAppDelegate = (TableViewAppDelegate *) [[UIApplication sharedApplication] delegate];
    
    	DetailViewControllerSenateNoTossUp *cnewTableViewFrameController = [[DetailViewControllerSenateNoTossUp alloc] initWithNibName:@"DetailView" bundle:nil];
    	UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:newTableViewFrameController];
    	
    
    
    	CGRect newTableViewFrame;
    	newTableViewFrame.origin.x = 0; 
    	newTableViewFrame.origin.y = 0;
    	newTableViewFrame.size.height = 200;
    	newTableViewFrame.size.width = 280;
    	
    	UIView *newTableView = [[UIView alloc] initWithFrame: newTableViewFrame];
    	newTableView = newTableViewFrameController.view;
    	
    	[self.view addSubview:newTableViewFrameController.view];
    
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    This is what pushViewController: is for. In your root view controller just create the detail view controller and use [self.navigationController pushViewController:newcontroller animated:YES] and it will use the same existing navigation controller.

    BTW, it's not polite to cross-post something that hasn't had much chance to be answered.
     
  3. computerartist thread starter macrumors newbie

    Joined:
    Sep 4, 2010
    #3
    I wasnt trying to cross post anything sorry, I was following up on a separate post that I had and was hoping he knew what was going on.



    As far as using pushViewController, I did not want the Navigation Controller to be pushed, I simply wanted to add a new tableView and be able to push that controller when clicking on the cell.
     
  4. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    You have a root view that has a navigation controller and a table view. User taps a row in the table view and you want to push another tableview. Is that correct?

    If so, this is a standard master-detail UI. You push the detail view controller in the didSelectRowAtIndexPath: method of the master view controller.

    You do not need to create a second navigation controller.

    Is this different in some way from what you're trying to accomplish?

    If you look at Settings.app its UI is based all around tables. Do you see something there that is the same as what you want to do?
     
  5. computerartist thread starter macrumors newbie

    Joined:
    Sep 4, 2010
    #5
    I have a root view that has a navigation controller and a table view.

    I have a UIButton and the user clicks the button and the second table view appears.

    Then I tap the second table view cell and I want the root view controller to be pushed.
     
  6. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #6
    If I understand this would be the same as tapping a back button on the navbar. You just want the second table view to slide offscreen. If so just use

    Code:
    [self.navigationController popViewControllerAnimated:YES];
     
  7. computerartist thread starter macrumors newbie

    Joined:
    Sep 4, 2010
    #7
    When I click the UIButton, I dont want a "Back" button to appear though.

    I want that Second table view though to then control that navigation controller when i click on a cell.
     
  8. computerartist thread starter macrumors newbie

    Joined:
    Sep 4, 2010
    #8
    I got it dude thanks for your time:

    Code:
    controller.tableView.delegate = self;
    controller.tableView.dataSource = self;
    
     
  9. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #9
    Do you have separate view controllers for each table view? If not I guess that you're just swapping the views. If so I wouldn't recommend doing it that way. But if you must then, yes, the single view controller needs to be the datasource and delegate for both tableviews.
     
  10. computerartist thread starter macrumors newbie

    Joined:
    Sep 4, 2010
    #10
    That didnt work with those 2 lines of code.

    If you wouldnt recomend doing it that way, how would you recomend doing it?

    Is it possible to do it the way I am wanting to do it?
     
  11. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #11
    I would simply use two view controllers, one for master and one for detail and I'd push the detail view controller. There would be a Back button. There could also be a reason to pop the view controller if a row is chosen, based on what your app does. I have a case like that.

    When you ask "Is it possible to do it the way I am wanting to do it?" I thought you had it working. No?

    At any rate, yes, it's possible to swap two table views, and, yes, it's possible to have a single view controller that manages two table views. Not having a clue what your app does or why you want to do this I guess I can't say whether that's a good idea or not. Without a good reason though I'd use two view controllers. It's simpler to implement, simpler for your users to understand and more standard.
     
  12. computerartist thread starter macrumors newbie

    Joined:
    Sep 4, 2010
    #12
    I thought I had it working too but it didnt work to simply try to set the first table view as the delegate and datasource. It switched back to the first view for some reason, I am sure it is my error.

    There is a good reason for it, I am showing two completely different variations of the same type of data and want them to be able to check them against each other.

    Any idea of how to set it to work with a single view controller that manages two table views when the second one appears?
     
  13. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #13
    If what you want is for only one of the tables to be visible at a time what I would do is create both tableviews. Add both to the view controller's view at the same place and hide one of them. To switch between them I would unhide one and hide the other.

    All of the connections between the tableviews and the view controller need to be set up at the start. All of the callbacks need to differentiate between the two tables so the correct values are returned to the two tables.

    One other thing, a table view is like a blank slate. If you call reloadData all of the contents of the table are updated. The only thing you can't change is whether the table is plain or grouped. You might be able to use a single table view just change the appearance.
     
  14. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #14
    Technically, only the visible cells are updated. :)
     
  15. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #15
    I guess depends what you mean by updated. All of the table geometry, number of rows and sections, names of sections, height of all the rows, etc. Everything is updated.

    I have an app that works with a single table view that is updated like this when a button is tapped in the table. The UI is in some ways like a web browser. There are buttons in the rows. Tapping a button updates the entire content of the tableview. It calls reloadData and fades to the new appearance. I have back/forward buttons in a toolbar. But it's a single tableview. The navigation stack doesn't change. There's a back button in the nav bar that doesn't change.
     
  16. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #16
    Yeah, I guess the data source methods for number of sections, rows, etc. are called for the entire table but cellForRowAtIndexPath: only for cells that are visible.
     
  17. computerartist thread starter macrumors newbie

    Joined:
    Sep 4, 2010
    #17
    That is what I dont know how to do is point both of the table views to the one navigation controller.
     
  18. computerartist thread starter macrumors newbie

    Joined:
    Sep 4, 2010
    #18
    So I got the second table view added so now i have 2 table views, any idea how to point the second one to the navigation controller?
     
  19. computerartist thread starter macrumors newbie

    Joined:
    Sep 4, 2010
    #19
    Code:
    	[self.tableView removeFromSuperview];
    	
    	DetailViewControllerSenateNoTossUp *controller = [[DetailViewControllerSenateNoTossUp alloc] initWithNibName:@"DetailView" bundle:nil];
    	UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller];
    	navController.view.frame = CGRectMake(0, 0, 320, 416);
    	
    	[controller release];
    	
    	CGRect frame;
    	frame.origin.x = 0; 
    	frame.origin.y = 100;
    	frame.size.height = 200;
    	frame.size.width = 280;
    	
    	UIView *try = [[UIView alloc] init];
    	try = controller.tableView;
    	try.frame = frame;
    
    	tableViewAppDelegate = (TableViewAppDelegate *)[[UIApplication sharedApplication] delegate];
    	[tableViewAppDelegate.window addSubview:try];
    
     
  20. computerartist thread starter macrumors newbie

    Joined:
    Sep 4, 2010
    #20
    Finally got it. Thanks for stickin' in there with me guys. I spent a long long time trying to figure that out and just needed some direction. I bet I spent 30 hours on it. Here is the full block of code that I got to work:

    Code:
    	DetailViewControllerSenateNoTossUp *controller = [[DetailViewControllerSenateNoTossUp alloc] initWithNibName:@"DetailView" bundle:nil];
    	UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller];
    	navController.view.frame = CGRectMake(0, 0, 320, 416);
    	
    	[controller release];
    	
    	CGRect frame;
    	frame.origin.x = 0; 
    	frame.origin.y = 100;
    	frame.size.height = 200;
    	frame.size.width = 280;
    	
    	UIView *try = [[UIView alloc] init];
    	try = controller.tableView;
    	try.frame = frame;
    	
    	[self.tableView addSubview:try];
    	self.tableView = controller.tableView;
    	self.tableView.delegate = self;
    
    The most important part of this is the last three lines. It must also be in that order. 1) Add the new table view to your current view. 2) Set the current table view to equal the new table view. 3) Set the current table as the delegate.

    Code:
    [self.tableView addSubview:try];
    self.tableView = controller.tableView;
    self.tableView.delegate = self;
    
     

Share This Page