1

I have an NSDictionary objectreturned from my server. I am using it to populate information on a UITableViewController. My issue is that when I am processing each object returned, it seems to duplicate, lets say I get 2 objects returned. It will make 2 table cells but with only the first object's data. So lets say the title section from both objects is "yes" and "no". It would populate the list with the first object title so it would show, yes and yes.

Here is what happens:

Controller is loaded:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // MAKE REQuEST TO SERVER
    [self makeRequests];

}

runs the request from server which stores information as follow:

NSDictionary* json = [NSJSONSerialization
                          JSONObjectWithData:returnData //1

                          options:kNilOptions
                          error:&error];
self.googlePlacesArrayFromAFNetworking = [json objectForKey:@"requested_data"];
[self.tableView reloadData];

than the cellForRowAtIndexPath runs:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"timelineCell";
    FBGTimelineCell *cell = (FBGTimelineCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (!cell)
    {
        cell = [[FBGTimelineCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.usernameLabel.text = [_googlePlacesArrayFromAFNetworking[indexPath.row] objectForKey:@"name"]; <!-- here is where the title example I was talking about happens
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        [cell initTimelineCell];
    }

    UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg", indexPath.row]];
    cell.photoView.image = img;

    return cell;
}

Here is my googlePlacesArr...

@property (strong, nonatomic) NSArray *googlePlacesArrayFromAFNetworking;

Here is my numberOfRowsInSection

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.googlePlacesArrayFromAFNetworking count];
}

Suggestions thoughts? Let me know if you need anything else.

UPDATE:

Also I think its important to note I am using this:

https://github.com/Seitk/FB-Gallery

As a base for populating my returned data.

0

2 Answers 2

2

This usually happens when the cell gets recycled if your tableView:cellForRowAtIndexPath: method fails to set some of the members of recycled objects.

That is precisely what happens in your code: when the cell is recycled, you never set its usernameLabel.text, so the recycled value may be shown multiple times.

You should move the code that sets up cell's properties outside of the if statement to fix this problem:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"timelineCell";
    FBGTimelineCell *cell = (FBGTimelineCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (!cell)
    {
        cell = [[FBGTimelineCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        [cell initTimelineCell];
    }
    // This line is moved from the "if" statement
    cell.usernameLabel.text = [_googlePlacesArrayFromAFNetworking[indexPath.row] objectForKey:@"name"];
    UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg", indexPath.row]];
    cell.photoView.image = img;

    return cell;
}
Sign up to request clarification or add additional context in comments.

2 Comments

just adding: for iOS7, as far as I know, you don't need to test for cell existence, nor alloc-init it. So for iOS7 I'd just remove the if-condition and also the alloc-init line.
Hey thanks for the quick reply! The issue is that still does not seem to do the trick.
1

Try this:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    /* ///////  Custom code //////  */
    cell.usernameLabel.text = [_googlePlacesArrayFromAFNetworking[indexPath.row] objectForKey:@"your_key_name"];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.