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 UITableView
documentation:
Important: You must register a class or nib file using the
registerNib:forCellReuseIdentifier:
orregisterClass:forCellReuseIdentifier:
method before calling thedequeueReusableCellWithIdentifier: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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With