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
Related
When I'm trying to insert section 0 using -insertSections:withRowAnimation: with UITableViewRowAnimationTop tableHeaderView affected by animation(It collapses and expanded to original size again). Other animation types work as expected.
[self.tableView beginUpdates];
if (changes.insertedSectionsIndexes.count > 0) {
[self.tableView insertSections:changes.insertedSectionsIndexes withRowAnimation:UITableViewRowAnimationTop];
}
[self.tableView endUpdates];
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];
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];
Hi I have created UITableView. On Touch of it cell, i am inserting more rows in it. After inserting more rows in UITableView, scroll is not working. This is only happening in ios 7.
Have any one came across this problem??
here is the code of insertion.
NSArray *rowIndex = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:1 inSection:[self.lastSelectedIndex section]]];
[self.table beginUpdates];
[self.table insertRowsAtIndexPaths:rowIndex withRowAnimation:UITableViewRowAnimationNone];
[self.table endUpdates];
i am not sure what does going on your code but if you want to scroll after every add row just call the method bellow (note :- modify the code as per your variable name)
-(void)goToBottom
{
NSInteger lastSectionIndex = MAX(0, [myTableName numberOfSections] - 1);
NSInteger lastRowIndex = MAX(0, [ myTableName numberOfRowsInSection:lastSectionIndex] - 1);
NSIndexPath *lastIndexPath = [NSIndexPath indexPathForRow:lastRowIndex inSection:lastSectionIndex];
if([myArray count] > 0)
{
[myTableName scrollToRowAtIndexPath:lastIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
}
Have you created the View with the storyboard? maybe you have to check your constraints.
if your table view occupies all of the space in the view, select it then go to "Editor>pin>top space to superview and bottom space to superview"
I am trying to memorize an index for an item (indexPath.item) in a UICollectionView and at a later time, after the view is replaced and then restored, to scroll to that memorized item.
When memorizing the item, indexPath, and indexPath.item are:
indexPath is: <NSIndexPath 0x1d87b380> 2 indexes [0, 32]
indexPath.item is: 32
When recalculating the indexPath later for that item, indexPath, and indexPath.item are:
indexPath is: <NSIndexPath 0x1d8877b0> 2 indexes [0, 32]
item is: 32
I try to scroll to the memorized location by using:
NSIndexPath *iPath = [NSIndexPath indexPathForItem:i inSection:0];
[self collectionView scrollToItemAtIndexPath:iPath atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:NO];
I receive an error:
attempt to scroll to invalid index path
Do you call [self.collectionView reloadData];
before trying to scroll to the indexPath?
If you want to relocate the indexPath when the reloadData finishes, place the code in a block like this:
[UIView animateWithDuration:0
animations: ^{ [self.collectionView reloadData]; }
completion:^(BOOL finished) {
NSIndexPath *iPath = [NSIndexPath indexPathForItem:i inSection:0];
[self collectionView scrollToItemAtIndexPath:iPath atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:NO];
}];
Before calling scrollToItemAtIndexPath, you could also check the number of items in section to make sure i is valid, or you still will get the same error.
Printing numberofItemsInSection revealed that the view it used for scrolling was stale. When I used the scrollToItemAtIndexPath after refreshing the view, it worked!