Setting Scroll Position in Uitableview

Setting scroll position in UITableView

I think I've got a similar app: A list of item., tap '+' -> new screen, come back and see the updated list, scrolled to show the added item at the bottom.

In summary, I put reloadData in viewWillAppear:animated: and scrollToRowAtIndexPath:... in viewDidAppear:animated:.

// Note: Member variables dataHasChanged and scrollToLast have been
// set to YES somewhere else, e.g. when tapping 'Save' in the new-item view.

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (dataHasChanged) {
self.dataHasChanged = NO;
[[self tableView] reloadData];
} else {
self.scrollToLast = NO; // No reload -> no need to scroll!
}
}

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if (scrollToLast) {
NSIndexPath *scrollIndexPath = [NSIndexPath indexPathForRow:([dataController count] - 1) inSection:0];
[[self tableView] scrollToRowAtIndexPath:scrollIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
}

I hope this helps. If you add something in the middle of the list, you could easily scroll to that position instead.

How to get the scroll position of a UITableView in real time

Small gotcha - in Swift 3 it's

func scrollViewDidScroll(_ scrollView: UIScrollView)

and the below is not going to be triggered :

func scrollViewDidScroll(scrollView: UIScrollView)

UITableView scrolling to specific position

This seemed to do the trick.

[theTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:savedScrollPosition inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
CGPoint point = theTableView.contentOffset;
point .y -= theTableView.rowHeight;
theTableView.contentOffset = point;

Remembering scroll position on UITableView

Sorted!

With a bit of inspiration from Desdenova, whilst at work I had a good think about it and realised what it could be. Remembering that I had a search bar, I had implemented the following code a few months ago to hide it:

[self.tableView setContentOffset:CGPointMake(0,-20) animated:NO];

In naivety I put that in viewDidAppear rather than viewDidLoad. Obviously, this did hide the search bar, but with it being in the wrong void command it did it every time the masterDetailView returned to the top of the stack. So, after moving the above code to viewDidLoad it now still hides it, but only the once.

I'm just spelling it out like this for other beginners, like myself, who may come across the same problem and may just save their sanity!

Thanks to you all for your ideas that helped me out.

+1 to you all!

how to set UITableView's scroll position to previous location when click load earlier items button

When you are on top of your table view before loading previous content, you are at 0 for contentOffset. If you want to be able to stay at the same scroll position after reloading the table view with previous messages row above, you will have to do this :

// Save the contentSize of your table view before reloading it
CGFloat oldTableViewHeight = self.tableView.contentSize.height;

// Reload your table view with your new messages

// Put your scroll position to where it was before
CGFloat newTableViewHeight = self.tableView.contentSize.height;
self.tableView.contentOffset = CGPointMake(0, newTableViewHeight - oldTableViewHeight);

That's it !

If your previous table view content size height was 10 and the new one was 19, you want to place your content offset to 9 (so 10 from the bottom like before the reloading) which is equal to 19 - 10.

How to set scroll position of UITableView on view's load?

You can use this method:

tableView.ScrollToRow (Foundation.NSIndexPath.FromItemSection (0, 0), UITableViewScrollPosition.Top, false);

This is the complete code:

tableView = new UITableView (UIScreen.MainScreen.Bounds);
this.View.AddSubview (tableView);

UILabel header = new UILabel (new CGRect (0, 0, tableView.Frame.Width, 50)){BackgroundColor = UIColor.Blue};
header.Text = "I'm header";
header.TextColor = UIColor.White;
header.TextAlignment = UITextAlignment.Center;
tableView.TableHeaderView = header;

TableViewSource mySource = new TableViewSource ();
tableView.Source = mySource;
tableView.ReloadData ();

tableView.ScrollToRow (Foundation.NSIndexPath.FromItemSection (0, 0), UITableViewScrollPosition.Top, false);

Hope it can help you.



Related Topics



Leave a reply



Submit