iOS scroll to a section in a UITable View - ios

does anyone know how to programatically scroll a table view to a section at index path. what i'm trying to do is have tableview with 2 sections and an if statement for example
if loading = true scroll the table view up to hide the first section
when finished loading scroll table view back down to show first section

Here is the snippet:
NSIndexPath * indexPath = [NSIndexPath indexPathForRow:row inSection:section];
[_tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];

Related

Why UITableview scrolling up while reloading data

I have a UITableview with 40 to 50 sections. It is expandable tableview when ever tapped on Section header am expanding that corresponding section if already expanded it will close. My issue is when ever I tapped on Section header requesting server for data and received data from server my Tableview automatically scrolled to top and this is happend after 20th section only. I could not understand whats going wrong.
NSIndexSet *sections = [NSIndexSet indexSetWithIndex:self.selectedIndexPath.section];
[self.tableview reloadSections:sections withRowAnimation:UITableViewRowAnimationFade];
If you do something which changes the height of some of the cells the table will scroll to a different position after the reload.
If you want the tableview to scroll to the top of the tapped section once the load has finished, what about something like:
NSIndexSet *sections = [NSIndexSet indexSetWithIndex:section];
[self.tableView reloadSections:sections withRowAnimation:UITableViewRowAnimationFade];
NSIndexPath *sectionPath = [NSIndexPath indexPathForRow:NSNotFound inSection:section];
[self.tableView scrollToRowAtIndexPath:sectionPath atScrollPosition:UITableViewScrollPositionTop animated:YES];

Load more to last position in UITableView

I try to Build UITableView That load every time a 5 object and when I notice that the scroll table in the last position I reload table data and this open the table from the top again.
how I can save the position and when table reload back to last cell I see ?
Use the below code before you reload the table data
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:(numberOfRowsInLastSection - 1) inSection:(numberOfSections - 1)];
By the above line you will get the position of last row of table.
Use this code after reloading the tableView
[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
This will scroll to already saved position.
I find a solution in this post - UITableView , Scroll to bottom on reload?
[postTableView reloadData];
NSIndexPath* ipath = [NSIndexPath indexPathForRow: oldSkip -1 inSection: 1];
[postTableView scrollToRowAtIndexPath: ipath atScrollPosition: UITableViewScrollPositionTop animated:YES];

UITableView scroll to element programmatically

I have array of 20 elements, and table view that can show only 5 of them. On start of view, I am selecting 1 item, and I want to show that item in table view by selecting it. When I choose one of first 5, selection is visible. When I select 5+ element, I want that element to be visible too by moving content offset. How can I scroll TableView to specific element from list inside tableview array? In which function of table view delegate do I do that?
I tried setting content offset, and scrollToRowAtIndexPath inside cellForRowAtIndexPath method, but it is not working correctly. Any ideas?
Calling scrollToRowAtIndexPath: at cellForRow will not work..First you need to reload data of tableview and then you need to call as below after selecting an item..
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:selectedRow inSection:0];
[self.tableView reloadData];
[self.tableView scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionTop animated:YES];
Hope it helps you..
Put your scrollToRowAtIndexPath inside ViewDidLoad or ViewWillAppear of the TableViewController.

How to show last add cell on UITableview whileloading view or reloading tableview?

all I am doing one chat application, i which i am using UITable view to display reply and response from user.in this case after some interval of time i am reloading my tableview to fetch new data from server. But the problem is that after adding new content to table view it will go at the bottom of table view and i have to scroll table view to see that one.or in other case whenever i am reloading my table it will show its first cell on view. Now my question is "is it possible to load last cell of UITableview after view gets load or reload table view?"
on search I find this line but it give me error
[sTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:sender.tag inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
what is sender.tag in this line? // use of undeclared identifier sender
This line working well but scrolling the page which i dont want
[table_readText scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES ];
Any idea or suggestion would be highly welcome.
Thanks in advance..
You can also look at the scroll view's setContentOffset:animated: method.
Going to the top would mean,
[self.tableView setContentOffset:CGPointZero animated:YES];
and the bottom would be,
CGFloat height = self.tableView.contentSize.height - self.tableView.bounds.size.height;
[self.tableView setContentOffset:CGPointMake(0, height) animated:YES];
SECOND option:
scrollToRowAtIndexPath:atScrollPosition:animated:
Scrolls the receiver until a row identified by index path is at a particular location on the screen.
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated

how to show dynamically added cell selected in uitableview

I'm adding cell into uitableview on some action event. The cell is added at the top position in uitableview. I want the newly added cell to be selected by default.
Just call the method selectRowAtIndexPath:animated:scrollPosition: of the table view after the cell is added, assuming the cell is added in index 0 of section 0:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewRowAnimationTop]

Resources