Loading records to a TableView – (More button)

I have like 100 records to display on a table. But i will be only displaying 10 records, and when the user clicks the last row (Which will have a button that will say load the next 10 records) the table will row the next 10 records.

I have displayed the more button on the last cell successfully. But i don’t know how to display 10 records at a time. My code so far;

  • How do I give a UITextView focus programmatically?
  • Moving reordering control to left
  • Emojis support in Apple push notification
  • How to add a right button to a UINavigationController?
  • Unable to read symbols warning after updating to 4.3.3
  • How can I use the PoDoFo library for annotating PDFs on iOS?
  • ViewDidLoad

    self.arrayThatContainsAllHundredRecords= [NSArray arrayWithArray:hundredRecordsArray];
    
    [self performSelector:@selector(addTheTableFooter:) withObject:nil afterDelay:0.5]; 
    

    addTheTableFooter

    UIView* footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
    
    [footer addSubview:moreButton]; // i have also added an event to the button click see below, and i have not shown it here.
    
    self.tableView.tableFooterView =footer;
    
    [self.mainWindow removeFromSuperview];
    
    [self.tableView reloadData];
    

    The above would Display the More button after displaying all 100 records.

    moreButtonClickAction

    -(void)moreButtonClickAction {
    NSLog(@"Suppose to load the next 10 records");
    }
    

    numberOfRowsInSection

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

    I need a way to edit this code, so i will only display 10 records at a time, and display the next 10 when the user clicks the more button. (Now the total records should be 20 – (previous 10 + the 10 records as a result of clicking the more button ))

    NOTE: I will be downloading all 100 records and saving it in self.arrayThatContainsAllHundredRecords in the viewDidLoad method.

    I believe that i have to make some change before executing [self.tableView reloadData];, as in tell to load 10 records.

    Well, say if i only had 53 records. Then the user will click the more button 5 times and on the 6th instance, the table should display only 3 records. How can this be handled in the tableview:numberOfRowsInSection: ?

    2 Solutions Collect From Internet About “Loading records to a TableView – (More button)”

    tableView:numberOfRowsInSection: is the method that determines how many rows will be shown, so the first step will be to change that. You also need a property in your view controller to keep track of how many rows should be visible. Something like this:

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return self.numberOfRowsVisible;
    }
    

    And when the more button is clicked, you have to increase that number and reload the tableView data.

    -(void)moreButtonClickAction {
        if (self.numberOfRowsVisible < self.maxNumberOfRows) 
            self.numberOfRowsVisible = MIN(self.numberOfRowsVisible + 10, self.maxRows);
        [self.tableView reloadData];
    }
    

    To display 10 rows at a time, your tableview:numberOfRowsInSection: should return 10 (instead of returning the total number of records in your source array).

    Your tableView:cellForRowAtIndexPath: should then handle the paging, adding the current page times 10 to the indexPath.row property, to determine which element in your source array to use to feed the cell view.

    When your “More” button is tapped, you should call [self.tableView reloadData]; there.

    Edit: your cellForRowAtIndexPath: method should look something like this:

    - (UITableViewCell *)tableView:(UITableView *)tableView 
            cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        static NSString *CellIdentifier = @"TesterCell";
    
        UITableViewCell *cell = [tableView 
            dequeueReusableCellWithIdentifier:CellIdentifier];
    
        if (cell == nil) {
    
            cell = [[[UITableViewCell alloc] 
                initWithStyle:UITableViewCellStyleDefault 
                reuseIdentifier:CellIdentifier] autorelease];
        }
    
        NSInteger dataIndex = indexPath.row + (pageNumber * 10);
        MyDataObject *data = (MyDataObject *)[self.arrayThatContainsAllHundredRecords 
            objectAt:dataIndex];
    
        NSString *cellText = data.LastName; // or whatever
    
        cell.textLabel.text = cellText;
    
        return cell;
    }