Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Object in array displaying twice in tableview

Hey guys I have an array with two separate objects with diff names. But in my tableview it displays the first object twice, suggestions on what i'm doing wrong?

can you post your code

//First View Controller

-(void)mapView:(MKMapView * )mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control

{



    PFQuery *query = [PFUser query];
    [query whereKey:@"gymName" equalTo:view.annotation.title];

    [query findObjectsInBackgroundWithBlock:^(NSArray *currentUsers, NSError *error)

     {

         GymInfoDetailViewController *gymInfoDetailVC =[self.storyboard instantiateViewControllerWithIdentifier:@"GymInfoDetailViewController"];        // 

         gymInfoDetailVC.gymUsers = currentUsers;
         NSString *gymLabelText = view.annotation.title;
         [self.navigationController pushViewController:gymInfoDetailVC animated:YES];
         [gymInfoDetailVC.gymLabel setText:gymLabelText];

     }];


//SecondViewController

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

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


    // Configure the cell...
    NSArray *userArray = [gymUsers valueForKey:@"username"];
    NSLog(@"%@",userArray.description);
   // NSLog(@"%@",[gymUsers description]);
    cell.textLabel.text = [userArray objectAtIndex:indexPath.row];
    return cell;
}

When I log it out my array logs twice, and I m guessing its taking the first object in both logs (which are the same thing) and put them in the table.

5 Answers

so you have an array of PFUsers called gymUsers correct? why not just get the tableView data straight out of that array like this:

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

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

    //get the user from the array
    PFUser *gymUser = [gymUsers objectAtIndex:indexPath.row];
    //set the text using the user's username
    cell.textLabel.text = [gymUser objectForKey:@"username"];
    return cell;
}

It still displays that first user twice

in viewDidAppear of your view controller, log hte array and verify that you do in fact have 2 different users with 2 different names in your array:

NSLog("Gym Users: %@", gymUsers);

are you sure your users dont have the same names?

Maybe I should i have added that my array is basically results from a query. It looks like dictionary data.

you have an array of PFUser objects. everything looks right to me

Ok when I log out both here are the results:
2014-04-29 16:25:40.231 NextUp[21664:15503] Gym Users: ( "<PFUser:khlOwzz2W2:(null)> {\n currentLocation = \"<PFGeoPoint: 0x906cab0>\";\n email = \"one@one.com\";\n friendsRelation = \"<PFRelation: 0x906ca90>(<00000000>.(null) -> _User)\";\n gymName = \"LAFitness Brookhaven\";\n username = one;\n}", "<PFUser:5vncwocPqD:(null)> {\n currentLocation = \"<PFGeoPoint: 0x90d23c0>\";\n email = \"mlee3223@gmail.com\";\n friendsRelation = \"<PFRelation: 0x906cbb0>(<00000000>.(null) -> _User)\";\n gymName = \"LAFitness Brookhaven\";\n username = Stat2315;\n}" )

ok so your array looks fine. you have 2 users with unique names. now, lets modify the cellForIndexPath a bit and try and pick apart whats really happening

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

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

    Log the array:
    NSLog(@"Gym Users array : %@", gymUsers);
    //get the user from the array
    PFUser *gymUser = [gymUsers objectAtIndex:indexPath.row];

    //Log the user
     NSLog(@"User: %@", gymUser);
    //Log the username
    NSLog(@"Username: %@", [gymUser objectForKey:@"username"]);


    //set the text using the user's username
    cell.textLabel.text = [gymUser objectForKey:@"username"];
    return cell;
}

what gets output when you run that?

I m sorry when I log out gymUsers ^^ those are the results