DevFly
DevFly

Reputation: 423

"Load more.." on tableView with custom cells - cell reusing is causing me some issues

I'm trying to implement "Load more..." on a tableView. I've done it, but I don't know if it's efficient. The thing is that I have custom cells, and if I do like this:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";

    ArticlesCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell==nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ArticlesCell" owner:self options:NULL];
        cell = (ArticlesCell *) [nib objectAtIndex:0];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    tableView.backgroundColor = cell.backgroundColor;

    if (indexPath.row <= (self.bookmarks.count - 1)) {
        [self configureCell:cell atIndexPath:indexPath];
    }else{
        cell.textLabel.text = @"Load more...";
    }

    return cell;
}

It works great but what happens is it's reusing the cells, and if I scroll, every fifth cell (this is height 77.0) will have the label "Load more...", but actually do it's job as normal. I found this workaround, but I don't know is it good and efficient.

Here it is:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";
    if (indexPath.row <= (self.bookmarks.count - 1)) {

    ArticlesCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell==nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ArticlesCell" owner:self options:NULL];
        cell = (ArticlesCell *) [nib objectAtIndex:0];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    tableView.backgroundColor = cell.backgroundColor;

        [self configureCell:cell atIndexPath:indexPath];
        return cell;
    }else{
        UITableViewCell *cell = [[UITableViewCell alloc] init];
        cell.textLabel.text = @"Load more...";
        return cell;
    }
}

As you can see I'm making the "Load more..." cell a simple UITableViewCell, and not reusing it, since it's only one. Is this good approach? Can you advice me in something better? Thank you!

Upvotes: 1

Views: 358

Answers (2)

Andy Bowskill
Andy Bowskill

Reputation: 1734

Another approach would be to use 2 different cell identifiers, one to identify and reuse (once initially created) an ArticlesCell and another to identify and reuse (once initially created) a "Load more..." UITableViewCell. At least then you will only create the "Load more..." UITableViewCell once rather than every time it scrolls into view.

static NSString *ArticleCellIdentifier = @"ArticleCell";
static NSString *LoadMoreCellIdentifier = @"LoadMoreCell";

The LazyTableImages Apple iOS sample project uses a similar approach (see the Classes/ RootViewController.m).

Upvotes: 1

Tendulkar
Tendulkar

Reputation: 5540

When you are click on loadmore button then increase the number of rows and reload the tableview . i.e in the method numberofrowsinsection.Let me know if you need any more

Upvotes: 0

Related Questions