user3228210
user3228210

Reputation: 1

Table View not populating objects

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"GenusNameCell";

    GenusNameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[GenusNameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"GenusNameCell"];


        cell.GenusNameLabel.text = [genusName objectAtIndex:indexPath.row];
        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];

    }

    return cell;
}

I have a tableview with an Array of objects but when I run it. Nothing shows up. Im fairly new to xcode but Im not sure what my mistake is in the code. Can some one help me out?

Upvotes: 0

Views: 65

Answers (2)

Bruno
Bruno

Reputation: 109

If the cellForRowAtIndexPath callback is never called, it could be:

  • you didn't have set the dataSource of your tableview;
  • you didn't implement the numberOfSectionsInTableView: and/or tableView:numberOfRowsInSection: callbacks.

And if you dequeue your cell, you need to set the text outside the if branch like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"GenusNameCell";

    GenusNameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[GenusNameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"GenusNameCell"];
        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    }
    cell.GenusNameLabel.text = [genusName objectAtIndex:indexPath.row];

    return cell;
}

Upvotes: 1

Maniganda saravanan
Maniganda saravanan

Reputation: 2198

Change the code like this your code will run,

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

static NSString *CellIdentifier = @"GenusNameCell";

GenusNameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[GenusNameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"GenusNameCell"];

    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];

}
cell.GenusNameLabel.text = [[genusName objectAtIndex:indexPath.row] geniusname];

return cell;
}

geniusname is the NSString you stored the name of the person.

Upvotes: 0

Related Questions