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?
You forgot to register the nib/class for the cell identifier.
Form the Apple
UITableViewdocumentation:Here is an example for a plain
UITableViewCell:If you do not register the nib/class the method
dequeueReusableCellWithIdentifier:forIndexPath:is essentially the same asdequeueReusableCellWithIdentifier:. And it will not automatically create an instance for you.