1

I am trying to implement a "Load More" cell into my UITableView.

How can I change content, height, etc. of the cell "Load More" programmatically? I assume that this can be done with getting the selected cell like shown below. When I want to change the background color for instance - it has no effects.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.row == 2){
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

}
Bins Ich
  • 1,822
  • 6
  • 33
  • 47
  • Have you tried [tableView reloadData]??? – PaulG Jul 26 '12 at 19:50
  • Yes I did. It has no effects. Besides when I scroll through the TableView the cell would be reloaded and `cellForRowAtIndexPath:` would be called and the cell has the same content as before. – Bins Ich Jul 26 '12 at 19:54

4 Answers4

2

call this function:

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation (UITableViewRowAnimation)animation

Something like this:

NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:3 inSection:0];
NSArray* rowsToReload = [NSArray arrayWithObjects:rowToReload, nil];
[UITableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationNone];

more info here.

Community
  • 1
  • 1
Dima
  • 23,484
  • 6
  • 56
  • 83
  • This actually works, although when I scroll up and down again the cell changes are gone again. But this isn't a problem yet. Any idea how I can change the cell height too? – Bins Ich Jul 26 '12 at 20:17
  • your `heightForRowAtIndexPath` function should be handling this. It should be using the data to decide how tall a cell should be. What is the logic inside it? – Dima Jul 26 '12 at 20:25
  • ah ok you are right, got it ;) - but this is tricky because you are not able to calculate this size dynamically. I think Ricard's approach is the best because I can set a flag and prove it with an if statement in `heightForRowAtIndexPath` and `cellForRowAtIndexPath`. – Bins Ich Jul 26 '12 at 20:29
  • Agreed, and I would've suggested something similar, but that was not the question you asked. – Dima Jul 26 '12 at 20:30
1

I think it is not a good approach to call the table view delegate messages directly... Maybe you would want to set a flag when you have to display one extra row for this "load more" cell view and at that point call the reloadData message of your table view (or insert/reload a single cell view).

Ricard Pérez del Campo
  • 2,387
  • 1
  • 19
  • 22
  • can you pleas provide some sample code how I can set such a flag? can I do this in `didSelectRowAtIndexPath:` ? – Bins Ich Jul 26 '12 at 20:24
  • 1
    Sure. If you want to display a "load more" cell when selecting the last cell, then, on your didSelectRowAtIndexPath: you could implement something like this: if ((indexPath.row == ([myDataSource nItems]-1)) { self.showLoadMore = YES; [self.tableView beginUpdates]; [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:(indexPath.row+1) section:(indexPath.section)]]]; [self.tableView endUpdates]; } And your tableView:numberOfRowsForSection: method should return total rows +1 when the flag is set to YES. – Ricard Pérez del Campo Jul 27 '12 at 06:44
  • He's not calling the delegate (nor the datasource), but the table. This will return for the cell at given index if it's visible, and is perfectly safe to call at anytime. – kra Jul 27 '12 at 08:21
0

if you want to change height, do it like this:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
        if(indexPath.row == 0){
            return 25;
        } else {
            return 120;         
        }
}

if you want to change color, do it like this

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
        if(indexPath.row == 0){
            cell.backgroundColor =  [[UIColor alloc]initWithRed:50.0/255.0 green:50.0/255.0 blue:100.0/255.0 alpha:1];
        } else {
            cell.backgroundColor = [UIColor whiteColor];
        }

//or use cell.textLabel.textColor to change the text color

}
John Ding
  • 1,350
  • 11
  • 20
0

If your load more is at the very bottom of the table, implement it as a table footer. You won't screw up your datasource with extra indices and you'll be free of flyweight to modify it/resize it as you see fit.

Something like: tableView.tableFooterView = theFooterView;

After loading the next page, if it was the last one, just remove the footer by setting the property to nil on the table.

About your snippiet: -cellForRowAtindexPath: (on the table view) will not return anything if the cell is not visible. Also, tableView:cellForRowAtIndexPath (the datasource version) will undo what you might have done when it gets called for that cell again. Put all cell modification code in your datasource, and call the appropriate reload cell method on the table when you want them to change.

kra
  • 214
  • 2
  • 9