2
votes

I am trying to make a simple iOS application that uses a UINavigationController two ViewController and a UITableView to display a list. The problem is that the table refuses to display the data from NSArray. All delegates are setup as you see in the pictures.

enter image description here

enter image description here

Every thing works if i create a xib file for the ViewController, but i dont need one more xib file if i can drag one more ViewController in Storyboard and to link it's class to my ItemListViewController as you see in the first picture.

ItemListViewController.h

#import 

@interface ItemListViewController : UIViewController{
    NSMutableArray *tblDataSource;
    IBOutlet UITableView *tblSimpleTable;
}

@property (assign) NSMutableArray *tblDataSource;
@property (retain) IBOutlet UITableView *tblSimpleTable;

@end

ItemListViewController.m

#import "ItemListViewController.h"

@interface ItemListViewController ()

@end

@implementation ItemListViewController

@synthesize tblDataSource,tblSimpleTable;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"bg.png"]];
    self.tblSimpleTable.backgroundColor = background;
    [background release];

    NSArray * data = [NSArray arrayWithObjects:@"Egg Benedict", @"Mushroom Risotto", @"Full Breakfast", @"Hamburger", @"Ham and Egg Sandwich", @"Creme Brelee", @"White Chocolate Donut", @"Starbucks Coffee", @"Vegetable Curry", @"Instant Noodle with Egg", @"Noodle with BBQ Pork", @"Japanese Noodle with Pork", @"Green Tea", @"Thai Shrimp Cake", @"Angry Birds Cake", @"Ham and Cheese Panini", nil];
    [tblDataSource setArray:data];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    NSLog(@"@%d",[tblDataSource count]);
    return [tblDataSource count];
}

- (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...
    UIImage *placeholder = [UIImage imageNamed:@"icon.png"];
    [cell.imageView setImage:placeholder];
    cell.textLabel.text = [tblDataSource objectAtIndex:indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"Clicked on row @%d",indexPath.row);
}

@end

If someone could point me in the right direction what am i doing wrong.

1

1 Answers

2
votes

In numberOfSectionsInTableView you're returning 0, which says there are no sections to generate (and therefore no rows, either). You probably want to return 1.

Also, you want to make sure you retain your NSMutableArray, so you really want to do:

@property (nonatomic, retain) NSMutableArray *tblDataSource;