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];
Related
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];
Currently when I reload the data in the table it stays in the same scroll position:
[self.tableView reloadData];
How can I get the scroll position/height to go back to the top/the first cell?
You can use the following method to scroll to any index path in your table view:
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
So, for the first cell, your indexPath would be:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
You can change the scrollPosition and animated parameters to get your desired effect/result.
Swift 4 version
let indexPath = IndexPath(row: 0, section: 0)
tableView.scrollToRow(at: indexPath, at: .top, animated: false)
I got this crash because I was trying to scroll to row 0 section 0 of an empty tableView. Make sure your tableData.count > 0 before you execute the scrollToRow().
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
I am inserting new section (section contains 3 cells) top of UITableView while SCROLLING TOP.
[_mainTable beginUpdates];
[_mainTable insertSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
[_mainTable endUpdates];
Section gets inserting properly. But it takes me top of Table i.e. cell 0 or row 0. I want this transaction to be smooth. I can insert
[_mainTable scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:1] atScrollPosition:UITableViewScrollPositionBottom animated:NO];
after endUpdates but it shows quick jerk because it takes you top of Table then suddenly scroll it to your last position.
How can i make it smooth.
Thanks
I haven't done exhaustive testing on this, but this seems promising:
Identify NSIndexPath of one of the visible cells.
Get its rectForRowAtIndexPath.
Get the current contentOffset of the table, itself.
Add the section, but call reloadData instead of insertSections (which prevents jarring scrolling).
Get the updated rectForRowAtIndexPath that you got in step 2.
Update contentOffset by the difference of the result from step 5 and that of step 2.
Thus:
[self.sections insertObject:newSection atIndex:0]; // this is my model backing my table
NSIndexPath *oldIndexPath = self.tableView.indexPathsForVisibleRows[0]; // 1
CGRect before = [self.tableView rectForRowAtIndexPath:oldIndexPath]; // 2
CGPoint contentOffset = [self.tableView contentOffset]; // 3
[self.tableView reloadData]; // 4
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:oldIndexPath.row inSection:oldIndexPath.section + 1];
CGRect after = [self.tableView rectForRowAtIndexPath:newIndexPath]; // 5
contentOffset.y += (after.origin.y - before.origin.y);
self.tableView.contentOffset = contentOffset; // 6
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];