0

I have the following method that should populate the cells of my UITableView with data from an array. I want to get the data from the array using the row that the data is being loaded into as the index.

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


    cellComments=(FullCommentCell *)[tableView dequeueReusableCellWithIdentifier:FullCommentCell_ID];
    if(cellComments==nil)
    {
        [[NSBundle mainBundle]loadNibNamed:@"FullCommentCell" owner:self options:nil];
        NSLog([NSString stringWithFormat:@"%i",indexPath.row]);
        [cellComments loadFullComments:[latestFMLComments objectAtIndex:indexPath.row]];
    }
    //cellComments.userInteractionEnabled=NO;
    return cellComments;

}

This is not working as expected. The table only ends up being populated with the first three elements of my array and then this data is reused until my table ends. The table should be using all the data from my array. Any idea why this is not working as expected?

2 Answers 2

3

You need to set the correct cell data each time you return a cell, whether it's new or reused. As you scroll down, cells from the top of the table are removed and reused for the bottom of the table. That's why you're seeing the first few data items repeated.

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

    cellComments = (FullCommentCell *)[tableView dequeueReusableCellWithIdentifier:FullCommentCell_ID];
    if (cellComments == nil) {
        [[NSBundle mainBundle]loadNibNamed:@"FullCommentCell" owner:self options:nil];

        // Do any one-time setup here, like adding subviews
    }

    // Set cell data for both new and reused cells here

    [cellComments loadFullComments:[latestFMLComments objectAtIndex:indexPath.row]];
    //cellComments.userInteractionEnabled=NO;

    return cellComments;
}
0
0

When you get the cellComments back from the dequeueReusableCellWithIdentifier call, you need to call loadFullCommnents again - cells are reused so you'll only have as many created as show up on screen.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.