blake305
blake305

Reputation: 2236

Why won't my UITableView Display?

I have googled and searched here on stackoverflow, but haven't been able to find a solution.

What I expect to happen: UITableView to display and show cells containing "hi"

What happens: UITableView doesn't even display

viewViewController.h

#import <UIKit/UIKit.h>

@interface viewViewController : UIViewController

@property (strong, nonatomic) IBOutlet UITableView *tableView;
//note: the above property is connected to my UITableViewController

@end

viewViewController.m

#import "viewViewController.h"

@interface viewViewController ()

@end

@implementation viewViewController
@synthesize tableView = _tableView;

- (UITableView *)tableView
{
    if (!_tableView){

        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    }
    return _tableView;
}

- (void)viewDidLoad
{
    [super viewDidLoad];


    //_tableView = [[UITableView alloc] init];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    //[self setTableView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"asking for rows");
    //#warning Incomplete method implementation.
    // Return the number of rows in the section.
    switch (section)
    {
        case 0:
            return 5;
        case 1:
            return 10;
            break;
        default:
            return 0;
            break;
    }
}

- (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];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.textLabel.text = @"hi";
    return cell;

}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"testtext";
}


@end

Please tell me what I have done wrong and correct my code.

Upvotes: 1

Views: 2198

Answers (3)

Tomasz Wojtkowiak
Tomasz Wojtkowiak

Reputation: 4920

  1. Add some protocols to your view controller:

    @interface viewViewController : UIViewController

  2. Check to be sure you’re connecting the delegate and datasource properties from tableView to File’s Owner.

Upvotes: 5

timthetoolman
timthetoolman

Reputation: 4623

It's not clear what you mean by the UITableView being connected to your instance variable. If you mean wired up from the XIB then you maybe getting "interference" from the overridden getter method that you have created. You may want to put an NSlog to see if that method is being called. if that is the case, the frame of the new table view that you are alloc/inviting is of zero size and I don't see anywhere in your code that you change the frame size.

good luck!

Upvotes: 1

Dean
Dean

Reputation: 939

Try subclassing UITableViewController instead of UIViewController.

Upvotes: 0

Related Questions