Question

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];
}
}
Was it helpful?

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.

OTHER TIPS

So I do something quite similar. You have an array with 70 strings (or dictionaries) to show in the table. The 'numberOfRowsInSection:' should be using an ivar which is initially set to 20.

When your button gets pressed, you increase the ivar by 20 (but to no more than the actual amount of data!), you reload the table, then you can scroll the table up to the "new" cell using tableView methods.

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