Question

I've a tableView in a UIViewController. When I load the viewController, is loading the tableView with 3 cells. Every time I make the scroll on my tableView and finish at the bottom of it, I wish that were added to it (other) 3 cells. Just how does the facebook app! Help me please! This is what I experienced in my code (the cells are not added):

    -(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        //I created an int flag because this method (as soon as it initialize the 
        //table) is called 2 times (because when it load the viewController, is called
        // the method that populates the arrays that contain the details to be shown in
        // the cells and reload the tableView)
        if (_flagCells == 0 || _flagCells == 1) {
            _flagCells ++;
            return;
        }

        if([indexPath row] == ((NSIndexPath*)[[tableView indexPathsForVisibleRows] lastObject]).row){

//phpClass is a class that contain scripts used to connect my app to script php for mySQL DB 
//objsRequest is a method of phpClass that receive a query for input and return an array of //results of that query. cellsLimit represent the cells to be shown (Once every 3 to 3) and //numberOfDequeuedCells represent the number of cells to be added (every 3) that is SELECT .. //FROM .. LIMIT 0,3 .... LIMIT 3,3 .... LIMIT 6,3 ....           
[_arrID addObjectsFromArray:[_phpClass objsRequest:[NSString stringWithFormat:@"SELECT ID FROM mii LIMIT %d,%d",_cellsLimit,_numberOfDequeuedCells]]];

            for (int i = (int)[_arrID count]-3; i < [_arrID count]; i++) {

//objRequest is a method of phpClass that receive a query for input and return a string that 
//represent the result of that query
                [_arrNames addObject:[_phpClass objRequest:[NSString stringWithFormat:@"SELECT Name FROM mii WHERE ID = %@",_arrID[i]]]];
                [_arrGen addObject:[_script objRequest:[NSString stringWithFormat:@"SELECT Gen FROM mii WHERE ID = %@",_arrID[i]]]];

//getImg is a method of phpClass that receive the user ID for input and return an image
//representing the user image
                UIImage *img = [_phpClass getImg:_arrID[i]];
                    [_arrImgs insertObject:img atIndex:i];

//Here is the problem because the rows doesn't be added to my tableView
                [_tableView beginUpdates];
                [_tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:i inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];

            }
            _cellsLimit +=3;
            [_tableView endUpdates];
        }
    }
Was it helpful?

Solution

I did that myself some time ago. If I remember right, then I just loaded additional data into the Array that I used as container for the data in the table. Once the data was loaded then I redraw the table. That's it.

I think you are getting lost just because you try to do it more complicated.

As for your comment:

[_tableView performSelector:@selector(reloadData) withObject:nil afterDelay:0.001];

or

 [_tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];

both should execute the reloadData in another thread and therefore should not cause a loop. However, even then you should add the new data just before the end of the table is reached.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top