views:

1053

answers:

5

I have a UITableView whose contents are dynamically changing, like a FIFO stack. Cells are added to the bottom and removed from the top.

This works beautifully, and I can scroll to the indexPath so that the newest message always scrolls down to the bottom (Like a chat application).

Now.. I want to add a footer to that table section. Instead of using

SrollToRowAtIndexPath

I would like to be able to scroll to the tableFooterView.

Any ideas how I can do that would be appreciated.

A: 

I haven't tried, but what happens when you scroll to the last row+1?

Another idea would be to always add a dummy entry at the end and make it have a different look so it's seen as a footer. Then you can always scroll to that.

mahboudz
You get an out of range error :( I should have mentioned that I tried that one already in the original post.
Kevin
A: 

Since UITableView is a subclass of UIScrollView, you can scroll to wherever you like using the UIScrollView method

- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated

Just set the rect so that when it's visible the footer is visible, and you'll have your solution (you can use the footer's rect or something else, just so long as you get the right behavior all the time).

Gordon Worley
+2  A: 

Maybe something like:

[tableView setContentOffset:CGPointMake(0, tableView.contentSize.height) animated:YES];
Michael Waterfall
+1  A: 

The easiest way to do this is to use UITableViewScrollPositionTop on the LAST ROW in the LAST SECTION. This works very well for me...

[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:LAST_ROW inSection:LAST_SECTION] atScrollPosition:UITableViewScrollPositionTop animated:YES];

Make sure your Table Footer View is well spaced out at the bottom and it should sit nicely animated inside the view...

Hope this helps...

iphone_developer
A: 

Great idea, was looking for this myself :) Here's sample code, which would do the trick:

[tableView reloadData];
NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:1];
[tableView scrollToRowAtIndexPath:index
    atScrollPosition:UITableViewScrollPositionBottom animated:YES];

You MUST have at least one cell in your tableView footer, the problem is that it's going to visible. Didn't have time to test, but I'd guess you could make it really small?

Additionally you must implement correct stuff inside numberOfSectionsInTableView (at least one for table and one for footer), numberOfRowsInSection (at least one for footer, your last section), viewForHeaderInSection (nil except for your cell), heightForHeaderInSection (maybe if you set this as zero), cellForRowAtIndexPath (add special case for your cell in footer)...

That should be enough.

JOM