shajem
shajem

Reputation: 2121

Loading more rows in the tableview

I need to load 15 records initially and then when the user clicks on the show more row of the cell, i need to load the next 20 or available records in my tableview. I am looking at this question posted in SO.

This functionality can be seen in appStore too. Where you load the next number of records.

Here the solution is;

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

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

I initialized and hard coded the value in the viewDidLoad for numberOfRowsVisible as 15.

self.numberOfRowsVisible=[NSNumber numberWithInt:15];

in numberOfRowsInSection - i am not sure if this is correct.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return MIN([self.arrayRecords count], 15);
}

in the moreButtonClickAction

-(void)moreButtonClickAction {
    if (self.numberOfRowsVisible < [self.arrayRecords count]) 
        self.numberOfRowsVisible = MIN(self.numberOfRowsVisible + 15, [self.arrayRecords count]);
    [self.tableView reloadData];
}

This is how i edited my code to suit the above suggestion left in SO. but this is not working. There seems to be some issue with MIN. I am not in MAC at the moment, so i can't remember the exact warning/error i got. So could someone kindly help me out.

Upvotes: 0

Views: 227

Answers (3)

Owen Hartnett
Owen Hartnett

Reputation: 5935

1) Don't use an NSNumber object, a plain integer will do. 2) Why not just have an integer variable that shows the current number of rows that you want to display, and initialize with 15, and keep it in your class instance. 3) You don't even have to use MIN:

return [self.arrayRecords count] < self.numberOfRowsVisible ? [self.arrayRecords count] : self.numberOfRowsVisible;

Upvotes: 1

ader
ader

Reputation: 5393

numberOfRowsInSection should return an integer. self.numberOfRowsVisible is not an integer.

viewDidLoad:

self.numberOfRowsVisible=15;

main implementation:

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

-(void)moreButtonClickAction {
    if (self.numberOfRowsVisible < [self.arrayRecords count]) {
        self.numberOfRowsVisible += 15;
        if (self.numberOfRowsVisible > [self.arrayRecords count]) {
            self.numberOfRowsVisible = [self.arrayRecords count];
        }
    }
    [self.tableView reloadData];
}

off the top of my head, code could do with re-factoring.

Upvotes: 1

mariusLAN
mariusLAN

Reputation: 1205

You have to use MAX instead of MIN if you want to show all possible entries.

Upvotes: 1

Related Questions