TableView in NavigationController

Discussion in 'iOS Development' started by acca2o, Nov 25, 2009.

  1. acca2o

    acca2o New Member

    Joined:
    Sep 26, 2009
    Messages:
    51
    Likes Received:
    0
    Device:
    2G iPod touch
    I'm trying to show a tableview inside a navigationcontroller.
    The application starts, shows the navigation-root-view-controller but it's empty, no tableview inside.
    If I put the same custom TableViewController in a tabBarViewController it works.

    What's the problem?
    Thanks



    this is my application code:

    [OBJC]#import "Application.h"
    @implementation Application
    - (void)applicationDidFinishLaunching

    Please Register or Log in to view images

    UIApplication*)application
    {
    //custom TableViewController
    TableViewController* tvc = [[TableViewController alloc] init];
    tvc.navigationItem.title = @"Title";

    //NavigationController
    UINavigationController* nc = [[UINavigationController alloc] initWithRootViewController:tvc];

    //UIWindow
    window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [window addSubview:nc.view];
    [window makeKeyAndVisible];

    //memory management
    [tvc release];
    [nc release];
    }


    - (void) dealloc
    {
    [window release];
    [super dealloc];
    }
    @end[/OBJC]


    and this the custom TableViewController:
    [OBJC]#import "TableViewController.h"


    @implementation TableViewController
    - (void)loadView
    {
    //data object
    source = [[Source alloc] init];

    //root TableView
    UITableView* tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain];
    tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
    tableView.delegate = self;
    tableView.dataSource = self;
    [tableView setEditing:YES animated:NO];
    [tableView reloadData];
    self.view = tableView;
    [tableView release];
    }


    - (NSInteger)numberOfSectionsInTableView

    Please Register or Log in to view images

    UITableView*)tableView
    {
    return [source numberOfSections];
    }


    - (NSInteger)tableView

    Please Register or Log in to view images

    UITableView*)tableView numberOfRowsInSection

    Please Register or Log in to view images

    NSInteger)section
    {
    return [source numberOfRowsInSection:section];
    }


    - (NSString*)tableView

    Please Register or Log in to view images

    UITableView*)tableView titleForHeaderInSection

    Please Register or Log in to view images

    NSInteger)section
    {
    return [source titleInSection:section];

    }


    - (UITableViewCell*)tableView

    Please Register or Log in to view images

    UITableView*)tableView cellForRowAtIndexPath

    Please Register or Log in to view images

    NSIndexPath*)indexPath
    {
    static NSString* MyIdentifier = @"MyIdentifier";
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil)
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
    cell.textLabel.text = [source objectAtIndex:indexPath.row];
    return cell;
    }


    - (UITableViewCellEditingStyle)tableView

    Please Register or Log in to view images

    UITableView*)tableView editingStyleForRowAtIndexPath

    Please Register or Log in to view images

    NSIndexPath*)indexPath
    {
    if(indexPath.row == 0)
    return UITableViewCellEditingStyleInsert;
    return UITableViewCellEditingStyleDelete;
    }


    - (void) dealloc
    {
    [self.view release];
    [source release];
    [super dealloc];
    }
    @end[/OBJC]
  2. lauNchD

    lauNchD Well-Known Member

    Joined:
    Jan 27, 2008
    Messages:
    1,844
    Likes Received:
    261
    Device:
    iPhone 5 (Black)
    Nothing seems to be popping out for me, but:

    • Maybe your Source is empty and the dividers aren't being shown.
    • Maybe it's not safe to release the navigation controller, since your window only retains its view, not the whole thing.
    • Is your TableViewController a UITableViewController or merely a UIViewController subclass? UITableViewControllers create their own table views in the default loadView method. You can then acquire your data source either in your init method or in viewDidLoad. (To make additional customizations to the table view, you can hook into viewDidLoad too)

    A few tips for better code+legibility:
    • Use iPTF's Objective-C syntax highlighting:
    HTML:
    [OBJC] [myCode doSomethingAwesomeWithClass: [UIView class]]; [/OBJC]
    turns into:
    [OBJC] [myCode doSomethingAwesomeWithClass: [UIView class]]; [/OBJC]
    • You can use your own "namespace" for your classes: UI* is for UIKit stuff, NS* is for foundation stuff, FB* is for Facebook Connect, so AC* could stand for your code. That makes them look more professional

    Please Register or Log in to view images

    and can avoid (rare) namespace collisions, if some MobileSubstrate library, for example, contains some kind of TableViewController class too.
  3. acca2o

    acca2o New Member

    Joined:
    Sep 26, 2009
    Messages:
    51
    Likes Received:
    0
    Device:
    2G iPod touch
    Thanks a lot for answer and tips, I'll use them!!! I already edited the previos post with OBJC

    About the custom TableViewController it's a subclass of UIViewController, because i read this way give me more control.
    Following your "maybes" I fixed the problem: releasing the NavigationController was the fault because no-objects retain it.

    Thanks again
  4. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    - (void)loadView {
    [super loadView];
    /* The rest of your code */
    }
  5. acca2o

    acca2o New Member

    Joined:
    Sep 26, 2009
    Messages:
    51
    Likes Received:
    0
    Device:
    2G iPod touch
    Do I realy need to call [super loadView]?
    What the UIViewController's loadView method does?
    I think it just allocs a generic empty UIView object.
    If I'm right maybe I don't need to call [super loadView].
    If I'm wrong I'd like to understand why.
  6. bddckr

    bddckr Active Member

    Joined:
    Dec 2, 2007
    Messages:
    1,434
    Likes Received:
    18
    Device:
    iPhone 4 (Black)
    Source
  7. Steaps

    Steaps New Member

    Joined:
    Oct 24, 2007
    Messages:
    5,074
    Likes Received:
    41
    Device:
    iPod touch
    Though it says this, I personally have noticed that with somethings you need it. Like creating arrays and such.
  8. bddckr

    bddckr Active Member

    Joined:
    Dec 2, 2007
    Messages:
    1,434
    Likes Received:
    18
    Device:
    iPhone 4 (Black)
    I just use viewDidLoad for this.
  9. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Whereas it makes substantially more sense for me to load all my view's subviews in the -loadView method. After all, this view is supposed to be equivalent to setting things up in a xib. In most xibs, the design is to stick a bunch of subviews to the viewController.view, a basic UIView.

    Creating and setting a new view in that method, while its intended purpose, is not really necessary when the view controller's view is a standard UIView. and is only really required when you want the default view to be a different subclass of UIView.

    In the op's case, he's defeating the purpose of adding a table as a subview of a UIViewController's view entirely, removing ALL the benefits that go along with such a scenario. Basically, the OP is creating a watered down UITableViewController. And to be completely honest, I didn't see the fact that the OP set the tableView as the controller's .view, rendering my advice moot.

    Generally, what I do is create a view controller, let it create its own view, and add the tableView as a subview of the table's view. I can then customize the view all I want while I manipulate the table independently.



    That actually would be wrong. When your view controller receives a memory warning, the unLoad (or whatever method) is called. That method is designed for you to unload what you need to. Most people don't do this. What the view controller does is it releases its view. The next time the view property is accessed, it needs to reload it. To do this, it calls -loadView, which recreates the arrays you allocated in -loadView, causing potential memory leaks.

Share This Page