How to scroll a UITableView to the tableFooterView - iphone

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.

I am using this to scroll to the footer view of a tableView:
[self.tableView scrollRectToVisible:[self.tableView convertRect:self.tableView.tableFooterView.bounds fromView:self.tableView.tableFooterView] animated:YES];

The best way to scroll a UITableView to the bottom of it's footerView is to simply set the content offset. You can calculate the bottom using the contentSize and the current bounds
Here is the way I do it.
CGPoint newContentOffset = CGPointMake(0, [self.instanceOfATableView contentSize].height - self.instanceOfATableView.bounds.size.height);
[self.instanceOfATableView setContentOffset:newContentOffset animated:YES];

Thanks to iphone_developer here is what I did :
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[tableView numberOfRowsInSection:0]-1 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
Then while I'm adding rows, I'm calling this and my tableView's footer view keeps being visible
Swift Version:
tableView.scrollToRow(at: IndexPath(row: self.tblview.numberOfRows(inSection: 0) - 1, section: 0), at: .top, animated: true)

So many poor answers. :-)
The BEST way:
[self.tableView scrollRectToVisible:
self.tableView.tableFooterView.frame animated:YES
];

Maybe something like:
[tableView setContentOffset:CGPointMake(0, tableView.contentSize.height) animated:YES];

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).

My late answer is for developer, who need to show footer when the keyboard is shown.
The right solution is to consider contentInset property (which can be changed after keyboard is shown), so it's super easy:
- (void)scrollToFooter {
UIEdgeInsets tableInsets = self.tableView.contentInset;
CGFloat tableHeight = self.tableView.frame.size.height - tableInsets.bottom - tableInsets.top;
CGFloat bottom = CGRectGetMaxY(self.tableView.tableFooterView.frame);
CGFloat offset = bottom - tableHeight;
if(offset > 0.f) {
[self.tableView setContentOffset:CGPointMake(0, offset) animated:YES];
}
}
I must notice, that in my case tableView was added to my own ViewController and one of cells have UITextField which become first responder. To move show footer when keyboard is shown you need to register keyboard did shown notification and (on iOS7) perform this method in the end of current run loop, coz in this case iOS7 automatically perform scrollToRowAtIndexPath after our method and footer will not be shown.
-(void)registerKeyboardNotification {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardDidShown:)
name:UIKeyboardDidShowNotification object:nil];
}
- (void)keyboardDidShown:(id)notification {
//move to the end of run loop
[self performSelector:#selector(scrollToFooter) withObject:nil afterDelay:.0];
}

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...

This good work for me!
CGRect footerBounds = [addCommentContainer bounds];
CGRect footerRectInTable = [tableview convertRect:footerBounds fromView:addCommentContainer];
[tableview scrollRectToVisible:footerRectInTable animated:YES];

This works for me in Swift 4
func addRow() {
tableView.beginUpdates()
tableView.insertRows(at: [IndexPath(row: array.count - 1, section: 0)], with: .automatic)
tableView.endUpdates()
DispatchQueue.main.async {
self.scrollToBottom()
}
}
func scrollToBottom() {
let footerBounds = tableView.tableFooterView?.bounds
let footerRectInTable = tableView.convert(footerBounds!, from: tableView.tableFooterView!)
tableView.scrollRectToVisible(footerRectInTable, animated: true)
}

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.

I'm using this:
- (void)scrollToBottom{
[self.myTable scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[self.fetchedResultsController.fetchedObjects count] -1 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

This work to me:
- (void)tableViewScrollToBottomAnimated:(BOOL)animated {
NSInteger numberOfRows = [self.tableView numberOfRowsInSection:0];
if (numberOfRows) {
if (self.tableView.tableFooterView) {
[self.tableView scrollRectToVisible:
self.tableView.tableFooterView.frame animated:YES
];
} else {
[self.tableView scrollToRowAtIndexPath:
[NSIndexPath indexPathForRow:numberOfRows-1 inSection:0]
atScrollPosition:UITableViewScrollPositionBottom
animated:animated
];
}
}
}

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.

Related

How can i set content offset on multiple scrollviews with one method?

I am having a little problem. I want to make all my scrollview scroll to the top when I press a UITableViewCell. Following is the code in didSelectRowAtIndexPath:
[self.tableView setContentOffset:CGPointZero];
[verbTableView setContentOffset:CGPointZero];
[seinhabenScrollView setContentOffset:CGPointZero];
[mdhPresensScroll setContentOffset:CGPointZero];
[mdhPreteritumScroll setContentOffset:CGPointZero];
[mhdScroll setContentOffset:CGPointZero];
....
There are more of those scrollview, and I want to put the all in one single object or something... I have tried following code:
for (UIScrollView *scrolls in topLayer.subviews)
{
[scrolls setContentOffset:CGPointZero];
}
Thanks!
The basic idea is right. It just depends upon how you identify the scroll views. You could do something like the following, which explicitly tests whether the subview is a kind of UIScrollView:
for (UIScrollView *scrollView in self.view.subviews)
{
if ([scrollView isKindOfClass:[UIScrollView class]])
scrollView.contentOffset = CGPointZero;
}
Or you can explicitly reference the specific scrollviews in question (in which case the class membership test isn't strictly needed):
for (UIScrollView *scrollView in #[seinhabenScrollView, mdhPresensScroll, mdhPreteritumScroll])
{
scrollView.contentOffset = CGPointZero;
}
Or, if you create an IBOutletCollection in IB, you can use that, too:
for (UIScrollView *scrollView in self.scrollViews)
{
[scrollView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];
}
(Note, in that final example, I'm scrolling to the location in question with animation, providing the user some visual cue as to what just happened; that's completely up to you.)
In a comment above, you say that topView has subviews which, themselves, have subviews that are scrollviews, you'd have to do something like the following to handle this subview-of-subview situation:
for (UIView *subview in topLayer.subviews)
{
for (UIScrollView *scrollView in subview)
{
if ([scrollView isKindOfClass:[UIScrollView class]])
scrollView.contentOffset = CGPointZero;
}
}
Avoiding the dynamic type check, just place the views that can scroll into an array. Then, a little fancier, add an extension to scroll view so they can scroll to zero using no params, that let's us do the scrolling with an NSArray one-liner (makeObjectsPerform...)...
#interface UIScrollView (ScrollToZero)
- (void)scrollToZero; // a no-param method for scrolling to zero offset
#end
#implementation UIScrollView (ScrollToZero)
- (void)scrollToZero {
[self setContentOffset:CGPointZero animated:YES];
}
#end
NSArray *allMyScrolls = #[verbTableView, seinhabenScrollView, // and so on. put ONLY scrollViews in here
// you can make this array a property, initialized when the views are created
[allMyScrolls makeObjectsPerformSelector:#selector(scrollToZero)];

scrolling other UITableview at the same time as another UItableview

I have a quite complicated view. What I basicly have is the following
You can see I have a vertical tableview. In each cell I have a horizontal tableview. What I want to do now is when I scroll one horizontal tableview, every other horizontal tableview should scroll also.
In my subClass of the vertical tableviewCell I have the following.
for(HorizontalTableCell *cell in mainTable.subviews){
if([cell isKindOfClass:[HorizontalTableCell class]]){
for(UITableView *cellTable in cell.subviews){
if([cellTable isKindOfClass:[UITableView class]]){
NSLog(#"cell table is table %#",cellTable);
[cellTable setContentOffset:CGPointMake(scrollView.contentOffset.x, 0) animated:NO];
}
}
}
}
But this is not working OK. Can anybody help me with this?
You will need to use the scroll view delegate methods. I'd suggest posting a Notification and then picking it up in the cells like so...
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"CellScrolledHorizontally" object:self.tableView];
}
Then in the horizontal cells you can observe the notification.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(scrolled:) name:#"CellScrolledHorizontally" object:nil];
- (void)scrolled:(NSNotification *)notification
{
UITableView *notificationTableView = notification.object;
if (notificationTableView == self.tableView)
return;
[self.tableView setContentOffset:notificationTableView.contentOffset];
}
Alternatively, use a UICollectionView.
Can u try this logic:
Instead of Horizontal tableviews use a UIScrollView. Now the base is a UITableView on which the horizontal scrollviews. Now use UIScrollview delegate methods for event handling.
Although this is not the solution but you can look at EASYTABLEVIEW

scrollToRowAtIndexPath not scrolling to inactive/unloaded cells

I've noticed that scrollToRowAtIndexPath:atScrollPosition:animated: doesn't scroll to cell that are not currently in view, so If I have 100 cells and I need to get to the one at 70, the call to that selector will do nothing.
Is there a way I can get that cell into memory? I already have the cell's index path...
I need to scroll to that position in my app when the user would want to go there.
Thanks for any thoughts!
EDIT: #dasblinkenlight
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide) name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillHide
{
//Load remote cell here then scroll
// :( dont know how to load remote cell yet
}
- (void)keyboardWillShow
{
//Load remote cell here then scroll
// :( dont know how to load remote cell yet
//_cellIndexPath gets assigned on didSelectRowAtIndexPath:
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:_cellIndexPath.row inSection:_cellIndexPath.section] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
EDIT2:
- (void)keyboardWillShow
{
//Load remote cell here then scroll
[NSThread detachNewThreadSelector:#selector(keyboardWillShowThreaded) toTarget:self withObject:nil];
}
- (void)keyboardWillShowThreaded
{
[NSThread sleepForTimeInterval:2.0];
[self performSelectorOnMainThread:#selector(keyboardWillShowMainThread) withObject:nil waitUntilDone:YES];
}
- (void)keyboardWillShowMainThread
{
//Get the cell
//_textFieldThatHasFirstResponder is a subview in the cell
//This returns null, probably because the cell is not loaded into memory
UITableViewCell *cell = [_textFieldThatHasFirstResponder superview];
NSLog(#"CELL TO SCROLL TO: %#",cell);
NSIndexPath *indexPathForCell = [self.tableView indexPathForCell:cell];
[self.tableView scrollToRowAtIndexPath:indexPathForCell atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}
OK, I've got the cause of this, see, you have:
NSIndexPath *indexPathForCell = [self.tableView indexPathForCell:cell]; // nil here
[self.tableView scrollToRowAtIndexPath:indexPathForCell atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
When you send indexPathForCell: for an out-of-view cell it returns nil, so tableView doesn't know where to scroll to.
you can implement a delegate so that you can call it from the class where you are in, so that is can update the position
"I've noticed that scrollToRowAtIndexPath:atScrollPosition:animated:
doesn't scroll to cell that are not currently in view, so If I have
100 cells and I need to get to the one at 70, the call to that
selector will do nothing. "
No. it is not true. I have just tried with a tableview with 100 rows. The following code works well.
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:78 inSection:0]
atScrollPosition:UITableViewScrollPositionNone animated:YES];
I don't think adding sleep is changing anything. It just delays execution but does not affect the order. Can you check if the index your are passing to scrollToRowAtIndexPath is valid? I remember seeing the same problem myself but it was related to invisible cell. It was impossible to retrieve invisible cell (tableView returned nil) and therefore its index path was nil and thus scrolling failed.
You could store locations of all cells or compute it on the fly and then pass it to
- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated;
It's the only solution I can imagine.

Call a method after [self.tableView endUpdates] ended

I want to add my sliderButton after the animation has ended but this doesn't work, it adds the button while the deletion of the section is being animated.
[coursesTable beginUpdates];
[coursesTable deleteSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationTop];
[coursesTable endUpdates];
[self.view addSubview:sliderButton];
Is there a possibility I can call a method that does that for me when the animation ended?
I believe that wrapping your table update in animateWithDuration would work:
[UIView animateWithDuration:0.0 animations:^{
[coursesTable beginUpdates];
…
[coursesTable endUpdates];
} completion:^(BOOL finished) {
// Code to run when table updates are complete.
}];
Other methods that I found suggested here on Stack Overflow did not work for me.
I used this technique at one time and tested it enough to verify that the completion block was called after I called the table's endUpdates method, but rewrote my code so I didn't need it any more before I had completely verified that the animation was actually finished.
You can find the answer here:
How to detect that animation has ended on UITableView beginUpdates/endUpdates?
Hope it helps!
I have also not found the solution of this.I have to use performSelector:withObject:afterDelay: for getting my work done.
[coursesTable deleteSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationTop];
after this the tableview again asks data from its datasource... so all the datasource methods including cellforRow is again called..
i suggest keeping a BOOL is Deleting.
so now do this
[coursesTable beginUpdates];
[coursesTable deleteSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationTop];
[coursesTable endUpdates];
Deleting = YES;
in cellForRow method
{
if (index path.row == lastrow ) // last rowcell is reached
if(Deleting)
{
//tableView has reloaded.
[self.view addSubview:sliderButton];
Deleting = NO;;
}
}

Scroll UITableView so that the header isn't visible

I've got a UITableView with a UISearchBar as the tableViews.tableHeaderView. Just like the new Mail.app, Notes.app, etc. in 3.0. I want to hide the SearchBar until the user drags it in his sight.
My attempt only works when there're a couple of items in the tableView, so that the tableView actually wants to scroll. I call this in loadView:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self._tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
Nevertheless it seems that Apple handles such a serachbar differently. After draging out the searchbar it doesn't seem to be bounded to the tablecells anymore (in Notes.app, not in Mail.app).
But perhaps Apple has a distinct method for that new 3.0 behaviour, and I just can't find it?
Maybe you can try it this way...
[self.tableView setContentOffset:CGPointMake(0,40)];
Worked for me too. I used the following:
[self.tableView setContentOffset:CGPointMake(0, self.searchDisplayController.searchBar.frame.size.height) animated:NO];
to query the height of the search bar.
This one gets you the exact same behavior as iPod.app:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
CGFloat searchBarHeight = CGRectGetHeight([[[self searchDisplayController] searchBar] frame]);
if ([[self tableView] contentOffset].y < searchBarHeight)
[[self tableView] setContentOffset:CGPointMake(0, searchBarHeight)];
}
This works for me.
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.bounces = YES;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.tableView setContentOffset:CGPointMake(0, 44)];
}
I had to scroll first to top then setContentOffset to 0, Then searchBar will be visible :
self.tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0), atScrollPosition: UITableViewScrollPosition.Top, animated: false)
self.tableView.setContentOffset(CGPointMake(0, 0), animated: false)
I kind of like doing it this way:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// Hide the table view header by default.
NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView scrollToRowAtIndexPath:index atScrollPosition:UITableViewScrollPositionTop animated:NO];
}
This way you don't really need to worry about how tall your header is. It just works!