Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populating Tableview Cells in iOS 6

I have tried searching up some tutorials on how to populate table views, but all I have found are old and outdated videos. I tried doing it from a slightly more recent one, and it does not work. I have

- (void)viewDidLoad
{
    [super viewDidLoad];
   [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
    [chemarray addObject:@"2"];
    [chemarray addObject:@"test"];
    [chemarray addObject:@"3"];
    [chemarray addObject:@"science"];  
}

and

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [chemarray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:    (NSIndexPath *)indexPath
{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"     forIndexPath:indexPath];

     cell.textLabel.text = [chemarray objectAtIndex:indexPath.row];
    return cell;
}

according to this tutorial, when run, it should display the items in my array, but for me, it does not! Does anyone know how to fix this?

like image 542
Alexyuiop Avatar asked Oct 25 '12 07:10

Alexyuiop


1 Answers

You forgot to register the nib/class for the cell identifier.

Form the Apple UITableView documentation:

Important: You must register a class or nib file using the registerNib:forCellReuseIdentifier: or registerClass:forCellReuseIdentifier: method before calling the dequeueReusableCellWithIdentifier:forIndexPath: method.

Here is an example for a plain UITableViewCell:

- (void) viewDidLoad {
   [super viewDidLoad];

   [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
}

If you do not register the nib/class the method dequeueReusableCellWithIdentifier:forIndexPath: is essentially the same as dequeueReusableCellWithIdentifier:. And it will not automatically create an instance for you.

like image 199
rckoenes Avatar answered Nov 04 '22 07:11

rckoenes