views:

59

answers:

3

Is this possible? I just want a small table in my current view... here is what I'm trying to do:

.h file

#import <UIKit/UIKit.h>


@interface IngredientsRootView : UIViewController <UITableViewDelegate, UITableViewDataSource> {
 UITableView *ingredientsTable;
}

@property (nonatomic, retain) UITableView *ingredientsTable;

@end

.m file I have delegate and data source methods and this code:

ingredientsTable = [[UITableView alloc] initWithFrame:CGRectMake(10, 10, 300, 300) style:UITableViewStylePlain];
 [ingredientsTable setDelegate:self];
 [ingredientsTable setDataSource:self];
 [self.view addSubview:ingredientsTable];

The app doesn't crash, but it doesn't show a table. At the moment I have just set everything definitely as per:

#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return 10;
}


// the appearance of table view cells.
- (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] autorelease];
    }

    // Configure the cell...
 cell.textLabel.text = @"Hello";

    return cell;
}

What am I doing wrong? Thanks

A: 

I guess it depends on where are you initializing the tableView, are you doing in the initWithNib: method of your controller?

rano
oh sorry.... no viewDidLoad :)
Thomas Clayson
try to link it as an IBOutlet in Interface Builder and leave the initialization to him, just keep the delegate/datasource part
rano
A: 

Try calling -reloadData on the table view after adding it as a subview.

Also, how is the view set up? Is it created in a XIB or via -loadView?

Jasarien
no XIBs... can't stand them ha. its created in viewDidLoad and added there. I'll try that. :)
Thomas Clayson
reloadData doesn't work... :(
Thomas Clayson
I meant the view to which the table view is added as a subview. How is that created? Have you implemented `-loadView`? If you're not using XIBs and you haven't implemented `-loadView`, or otherwise set the view controller's view elsewhere then `self.view` will return nil when you try and add the table view as a subview, and nothing will happen.
Jasarien
Also, keep in mind that `viewDidLoad` is only called after `loadView` or if the view was set elsewhere or created in IB. Set a breakpoint in `viewDidLoad` to verify that it is being called.
Jasarien
the app is a navigation controller app and the root view is the app delegate with the whole [window addSubview:] nonsense. Then this view has been pushed on top of the stack. Its within this view that I want to add a table view.
Thomas Clayson
I hate to sound pushy but that doesn't explain where the view controller's view is created. As I understand, you have a view controller that you are pushing onto a stack, and you wish to add a table view as a subview of the view managed by your view controller? Your view controller needs to create a view. This can be done by implementing loadView, and creating a view instance and assigning it to the view property, by setting the view property from some other class or by creating a view in interface builder and linking it to the view outlet. Have you done any of these?
Jasarien
If you haven't then you view controller doesn't have a view and `viewDidLoad` will not be called because no view is being loaded, and therefore your table view is not getting added as a subview.(sorry for double comment, ran out of space in the last one)
Jasarien
you were kinda right (sorry for getting back late)... the problem wasn't that I wasn't creating a view, it was where I was building everything from - the table view WAS getting added, but behind the view in front (an image view). Putting everything into loadView helped. :) thanks
Thomas Clayson
A: 

Did you remembered to add the [window addSubview:[IngredientsRootView view]]; to your app delegate ?

And by the way, instead of inheriting from UIViewController you can just inherit from UITableViewController and you'll get all that functionality for you with no added code.

Idan
`UITableViewController` assumes its `view` property is actually a `UITableView`, which makes it an inappropriate class from which to inherit, if your view is something else (e.g. a `UIView` "container")
Shaggy Frog
You're right - for some reason I thought his controller's view is the table view.Anyway, my first advice still stands.Thanks
Idan