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().
Related
I have a a tableView that I want to show on screen at a specific indexPath.row.
For example, say each tableViewCell takes up half of the screen, and I want the screen to appear at indexPath.row of 6, instead of always starting at indexPath.row of 0.
Is there any way to do this?
To make clearer, I want to have the table view appear scrolled such that rows 0-5 are scrolled off-screen, and row 6 is the top row in the table view.
You can use scrollToRowAtIndexPath:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:N inSection:M];
[yourTableView scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionTop
animated:YES];
You can experiment with these positions until you are happy with the results
UITableViewScrollPositionNone
UITableViewScrollPositionTop
UITableViewScrollPositionMiddle
UITableViewScrollPositionBottom
In Swift you can use something like this:
func scrollToSelectedPosition() {
let indexPath = NSIndexPath(forRow: selectedPositionInt!, inSection: 0)
self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Top, animated: false)
}
Where selectedPositionInt is the position you want to scroll to.
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];
I am going to make a tableview with 2 sections inside it. I can add cells to every section programmatically and when i add, i scroll to the end of tableview using
[_tableView setContentOffset:CGPointMake(0, CGFLOAT_MAX)];
My question is how can i scroll to the end of the section 0, so that when user add a cell to section 0, tableview scroll dynamically to the last cell in section 0.
thanks.
You can try with this code:
int yourSection = 2;
int lastRow = [tableView numberOfRowsInSection:yourSection] - 1;
[tableView scrollToRowAtIndexPath:[NSIndexPath lastRow inSection:yourSection] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
You get the numbers of rows in your section, then scroll to that indexPath.
All the suggestions above are correct, at least based on the docs. But it did not work in my case - I could not get the scroll to show the last row in the table view. I had to add a delay in scrolling to make that work.
- (void)scrollToTheBottom:(BOOL)animated
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:rowCount-1 inSection:0];
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:animated];
}
I called the above as:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self scrollToTheBottom:YES];
});
when you inserting row at end you have its index path, you can use scrollToIndexPath method of tableview to scroll
[self.liveChannelsTable scrollToRowAtIndexPath:IndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
Just in case, this is solution in Swift:
extension UITableView {
func scrollToBottom(animated: Bool = true) {
let sections = self.numberOfSections
let rows = self.numberOfRowsInSection(sections - 1)
self.scrollToRowAtIndexPath(NSIndexPath(forRow: rows - 1, inSection: sections - 1), atScrollPosition: .Bottom, animated: true)
}
}
In Swift 3 it's been updated to:
let indexPath = IndexPath(row: self.numberOfRowsInSection(0) - 1), section: 0)
self.commentsTableView.scrollToRow(at: indexPath, at: .bottom, animated: false)
In iOS 7, I use the following code to scroll to the top of my UITableView. You have to account for the overlap of the translucent status bar and navigation bar.
[tableView
setContentOffset:CGPointMake(
0.0,
-tableViewController.topLayoutGuide.length
)
animated:YES
];
This works only works after the first time you call it. On the first time you call it, my table gets scrolled much farther than it should, showing a lot of white space. Additionally, the UIRefreshControl appears frozen. You have to nudge the table a little to make it bounce back to the true top. Afterwards, you can call this code as many times as you want and it behaves as you'd expect it.
I've tried other ways, but they all have problems. The iOS 6 way behaves just as oddly on the first call. Although it doesn't jump a huge amount on subsequent calls, they are not correct because it scrolls to 64.0 points below the top of the table because we forgot to account for the status and navigation bar.
[table setContentOffset:CGPointZero animated:YES];
I've also tried scrolling to the first cell, but it doesn't scroll to the very top in one call. It will only scroll up one page's worth every time you call it.
[tableView
scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]
atScrollPosition:UITableViewScrollPositionTop
animated:YES
];
Try this one:
NSIndexPath* top = [NSIndexPath indexPathForRow:NSNotFound inSection:0];
[tableView scrollToRowAtIndexPath:top atScrollPosition:UITableViewScrollPositionTop animated:YES];
In SWIFT
let top = NSIndexPath(forRow: NSNotFound , inSection: 0)
tableView.scrollToRowAtIndexPath(top, atScrollPosition: .Bottom, animated: true)
Swift 4.0 and above
let top = NSIndexPath(row: NSNotFound, section: 0)
tableView.scrollToRow(at: top as IndexPath, at: .bottom, animated: true)
i looked at other answers and found the following solution that worked for me.
-(void) scrollToTop
{
if ([self numberOfSectionsInTableView:self.tableView] > 0)
{
NSIndexPath* top = [NSIndexPath indexPathForRow:NSNotFound inSection:0];
[self.tableView scrollToRowAtIndexPath:top atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
}
Hope, it solves your problem.
it's not "magic". The 64 offset is for status bar(20 points) and navigation bar(44 points) heights. If the scrollview have offset of 0 and you also have status bar + navigation bar, it would be under these objects and you will see -64.0 of your original content. In storyboard there is an option "Adjust scroll view insets" and this is checked by default
Try:
[tableView
setContentOffset:CGPointMake(
tableView.contentOffset.x,
-tableView.contentInset.top
)
animated:YES
];
Please try this:
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;
The sample is like this:
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:0 animated:YES];
In Objective-C
[mainTableView setContentOffset:CGPointZero animated:YES];
And in Swift:
mainTableView.setContentOffset(CGPointZero, animated:true)
In Objective C
NSIndexPath* top = [NSIndexPath indexPathForRow:NSNotFound inSection:0];
[self.tableView scrollToRowAtIndexPath:top atScrollPosition:UITableViewScrollPositionTop animated:YES];
In Swift
var scrollIndexPath: NSIndexPath = NSIndexPath(forRow:NSNotFound , inSection: 0)
self.tableview.scrollToRowAtIndexPath(scrollIndexPath, atScrollPosition: UITableViewScrollPosition.top, animated: true)
iOS scrollview is behaving a bit oddly. The 64.0 offset was added "magically" to the first scrollview in the view hierarchy "the first time" as you mentioned. I haven't figured out why this was happening. At the moment I only had a really hackish solution: you can add a dummy scroll as the first scrolling in the view hierarchy, with height set as 0. After that, you solution should work as usual.![enter image description here][1]
screenshot : http://imgur.com/LvrbcqG
Hope this helps.
Anyone else know why this is happening in the first place ?
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!