Search code examples
objective-ciosuitableviewloaddata

Display certain numbers of row at one time, click Button, display more


i m quite stucking with this problem. i have a NSArray *list holding 70 elements from a plist file. now i need to populate them to UITableView. what i want is to display 20 items at a time, then click the 21st cell to display another 20.. then press the 41st cell to display another 20 items... and so on till all 72 cells reveal..

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    return 20;

}



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

static NSString *addCellID = @"addMoreCell";
UITableViewCell *addCell = [tableView dequeueReusableCellWithIdentifier:addCellID];

if (indexPath.row == 0) {
    static NSString *CellIdentifier = @"labelCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    return cell;
} else if (indexPath.row < 25){
    NSDictionary *dic = [self.list objectAtIndex:indexPath.row];
    cell.textLabel.text = [dic objectForKey:@"Title"];
    cell.detailTextLabel.text = [dic objectForKey:@"Tips"];
    return cell;
} else {
    if (cell == nil) {
        addCell.textLabel.text = @"Load More...";
        return cell;
    }
}
}


- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == 20)
{
    [self.tableView reloadData];
}
}

Solution

  • Take an array, in which you will add 20 elements from plist, and

    set numberOfRowsInSection = [array count] and

    check if numberOfRowsInSection < totalNumberOfRecords in plist, if it is less, then increment numberOfRowsInSection by 1.

    And in cellForRowAtIndexPath check if (indexPath.row < totalNumberOfRecords -2 && indexPath.row < numberOfRowsInSection-1)

    then add "LoadMore Cell" else not.