HillInHarwich
HillInHarwich

Reputation: 449

My UITableView shows the same item on every row

I have a UITableView which I want to populate with details from an array of objects. The tableview shows the same item on every line (the correct number of lines though!) I know this must be an easy one - but I can't see where I've gone wrong:

Code snippet of view that initializes the table data:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

{

    if([segue.identifier isEqualToString:@"Show Tank List"])

    {

        NSURL *myUrl = [[NSURL alloc]initWithString:@"http://localhost/~stephen-hill9/index.php"];
        NSData *data = [[NSData alloc] initWithContentsOfURL:myUrl];
        NSError *error;
        NSArray *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
        int i;
        NSMutableArray *tanksList;
        tank *thisTank = [[tank alloc] init];
        tanksList = [[NSMutableArray alloc] init];
        for (i=0; i<json.count; i++) {
            NSDictionary *bodyDictionary = [json objectAtIndex:i];
            thisTank.tankNumber = [bodyDictionary objectForKey:@"ID"];
            thisTank.tankProduct = [bodyDictionary objectForKey:@"Product_Desc"];
            thisTank.tankPumpableVolume = [bodyDictionary objectForKey:@"Pumpable"];
            [tanksList addObject:thisTank];
        }
        [segue.destinationViewController setTanks:tanksList];
    }
}

...and the code that loads the table in the next view...

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;//keep this section in case we do need to add sections in the future.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [self.tanks count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Tank List Table Cell";
    UITableViewCell *cell = [self.tankTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithFrame:CGRectZero];
    }
    tank *thisTank = [self.tanks objectAtIndex:indexPath.row];
    cell.textLabel.text = thisTank.tankNumber;
    return cell;
}

Upvotes: 0

Views: 697

Answers (2)

taus-iDeveloper
taus-iDeveloper

Reputation: 701

reload the table every time!!!

[self.tableView reloadData];

Upvotes: 0

jrturton
jrturton

Reputation: 119242

Move this:

tank *thisTank = [[tank alloc] init]; 

Inside your for loop. You're updating the same object over and over again.

Also, you're initialising the cell wrong - use the designated initialiser, and pass the reuse identifier in, otherwise you will create new cells all the time:

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

And you really should follow objective-c naming conventions. Classes begin with upper case letters, everything else begins with a lower case letter. It makes your code much easier to read, for other people anyway.

Upvotes: 3

Related Questions