re-using a tableview

Discussion in 'iOS Programming' started by darthtroll, Sep 21, 2010.

  1. darthtroll macrumors newbie

    Joined:
    Aug 24, 2010
    #1
    Hi all,

    I want to re-use a tableview across different sections of a tar bar controller. So I want to basically define only once how the subviews are split up and the logic of how they're built etc. I then want to just be able to pick up this tableview, add the data, and have the table be built when it's being loaded. I want to have custom navigation controller items across each view that has these tableviews. So you might imagine for example, a tableview that has a list of recipes, another instance of that tableview that is the "favorites" section, and another tableview that has a "my custom recipes" section, etc. How is this done best? I could create the tabBarController programmatically in the rootViewController, along with the navigationController, and add the data at the same time there as well, but gathering the data and storing it that "early" in that manner would mean that views that don't even get loaded are using up more memory than is necessary.

    i.e. (copied from iOS reference library)

    Code:
    tabBarController = [[[UITabBarController alloc] init] autorelease];
    MyViewController1* vc1 = [[[MyViewController1 alloc] init] autorelease];
    
    MyViewController2* vc2 = [[[MyViewController2 alloc] init] autorelease];
    MyViewController3* vc3 = [[[MyViewController3 alloc] init] autorelease];
    
    MyNavRootViewController* vc4 = [[[MyNavRootViewController alloc] init] autorelease];
    
    UINavigationController* navController = [[[UINavigationController alloc] initWithRootViewController:vc4] autorelease];
    
    NSArray* controllers = [NSArray arrayWithObjects:vc1, vc2, vc3, navController, nil];
    
    tabBarController.viewControllers = controllers;
    
    The other option is that I can have multiple classes made that use the same copy & pasted tableview with the same subview logic but use different data. Please advise what the best design practice is.

    Darthtroll
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    You might want to consider "lazy-loading" then.
     
  3. Sykte macrumors regular

    Joined:
    Aug 26, 2010
    #3


    A couple things, if you're following MVC architecture you wouldn't create one UITableViewController for more than one view. Following apples UINavigation rules, you shouldn't have a UITabBar inside of a UINavigationController. You can however have a UINavigationController inside of a UITabBar.


    edit:

    Sorry noticed you are placing the other controllers inside the tabbar, forget that last point.
     
  4. darthtroll thread starter macrumors newbie

    Joined:
    Aug 24, 2010
    #4
    Apologies, but isn't lazy-loading referring to loading only the data that can be displayed on the screen?

    My issue is that I don't want to make a NSURLConnection and grab multiple sets of data from a web service directly in the rootViewController if I use the approach in my above post. It may be possible that the user may only load one tableview, and that data can be retrieved when that view is loaded, but grabbing data for 6-7 other views for example, would be expensive and wasteful. I want to try and use a single tableview across a tabBarController that each load different data, but that data is only loaded when that view is loaded. Is this possible to do, or do I need to create 6-7 near-identical views that have near-identical code?

    Darthtroll
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    No, lazy-loading is a design pattern where you defer loading the data until you need it. Doesn't necessarily have to be for the screen.

    Views need to be loaded before their viewControllers can be added to a navController or a tabController. But that's not necessarily where you have to load the data. You can defer it until the view appears or even, in your case, the table cells are populated. There may be UI lag if you try to load this data synchronously, depending on how long it takes and where you load it. Something to be aware of and may prompt you to consider asynchronously loading the data.

    It is possible without needing to duplicate so much code. You just may need to consider separate data-stores and / or perhaps some flags to trigger different selections of a single data-store.
     
  6. darthtroll thread starter macrumors newbie

    Joined:
    Aug 24, 2010
    #6
    Just letting any bloggers who read this in the future know that I decided to use one tableview with a switch to pick the source of the data. Thank you for all your help dejo.

    Darthtroll
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    You're welcome. Glad to help.
     

Share This Page