mufc mufc - 1 year ago 81
iOS Question

Data not loading in programmatically created tableView

I am trying to create an extremely simple app without using storyboards for the first time.

I am just trying to display a tableView as my first screen once the app loads. I create a UIView class and then create a table view in the viewDidLoad method.

Within my delegate class I create an instance of my class, and then set it as my root view controller.

When I run the app, the tableView displays, but the cells do not display the numbers on them. I have been reading the apple documentation on this but have not found a solution so far. It may be very small but I can't seem to figure it out.

As you can see I create an array filled with very simple strings that are just numbers. I want each number to display on a cell in the table view, but when the tableview loads the cells are blank.

#import "NumbersViewController.h"

@interface NumbersViewController ()


@implementation NumbersViewController

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

- (void)viewDidLoad
[super viewDidLoad];

UITableView *tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain];
tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
tableView.delegate = self;
tableView.dataSource = self;
[tableView reloadData];

self.view = tableView;

self.numArray = [NSMutableArray array];
[self.numArray addObject:@"1"];
[self.numArray addObject:@"2"];
[self.numArray addObject:@"3"];
[self.numArray addObject:@"4"];
[self.numArray addObject:@"5"];
[self.numArray addObject:@"6"];
[self.numArray addObject:@"7"];
[self.numArray addObject:@"8"];
[self.numArray addObject:@"9"];

// Do any additional setup after loading the view.

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

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Number of rows is the number of time zones in the region for the specified section.

return [self.numArray count];

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @"MyReuseIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];

NSString *number = [self.numArray objectAtIndex:indexPath.row];
cell.textLabel.text = number;
return cell;


Answer Source

You add data after you reloadData

//Delegate methods called
[tableView reloadData];

self.view = tableView;

// data added
self.numArray = [NSMutableArray array];
[self.numArray addObject:@"1"];

Solution Move numArray initialization & seeding code above reloadData

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download