Reputation: 3789
How would you go about forcing a UITableViewCell to scroll to the top if the tableview contains less than 10 or so cells? I support editing of Managed Object Contexts within my tableView cells while the tableview is in editing mode. Needless to say, if a cell is at the bottom of the tableview, it gets blocked by the keyboard when a user goes to edit the title or location of an event. I tried a solution like:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
if(_selectedIndex == indexPath.row){
_selectedIndex = -1;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
return;
}
if(_selectedIndex >= 0){
NSIndexPath *previous = [NSIndexPath indexPathForRow:_selectedIndex inSection:0];
_selectedIndex = indexPath.row;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:previous]
withRowAnimation:UITableViewRowAnimationFade];
}
_selectedIndex = indexPath.row;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
[tableView setContentOffset:CGPointMake(0, [tableView rowHeight]*indexPath.row) animated:YES];
}
But this does not keep the tableview at the contentOffset. it will snap to the top and then snap back
Upvotes: 3
Views: 12179
Reputation: 1
More specified answer to your question:
extension UITableView {
func scrollToBottom(){
DispatchQueue.main.async {
let indexPath = IndexPath(
row: self.numberOfRows(inSection: self.numberOfSections -1) - 1,
section: self.numberOfSections - 1)
self.scrollToRow(at: indexPath, at: .bottom, animated: true)
}
}
func scrollToTop() {
DispatchQueue.main.async {
let indexPath = IndexPath(row: 0, section: 0)
self.scrollToRow(at: indexPath, at: .top, animated: false)
}
}
}
Upvotes: 0
Reputation: 1
It will be helpful to scroll to top your tableView
tableView.setContentOffset(CGPoint(x: 0, y: -tableView.contentInset.top), animated: true).
Upvotes: 0
Reputation: 70
This works in my case, also it scrolls to headear's top If you have:
**myTablView.scrollRectToVisible((myTablView.rectForHeader(inSection: 0), to: myTablView.superview).0, animated: true)**
https://developer.apple.com/documentation/uikit/uitableview/1614872-rectforheader
Upvotes: 0
Reputation: 21
Have you try this? :
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;
The sample is:
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:0 animated:YES];
Upvotes: 0
Reputation: 4675
UITableView is a subclass of UIScrollView, so you can also use:
[mainTableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];
Upvotes: 2
Reputation: 39480
I like the following solution because it also scrolls to the top of a header view if you have one.
[self.tableView setContentOffset:CGPointZero animated:YES];
Upvotes: 12
Reputation: 9039
You probably should use this method on UITableView:
-(void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated
Essentially what you do is that when the [tableView:didSelectRowAtIndexPath:] method of your UITableViewDelegate gets called, you pass the index of that selection to the above method and set scrollposition = UITableViewScrollPositionTop.
That will scroll the selected cell to the top of the screen and out of the way of the keyboard.
Upvotes: 4
Reputation: 23634
Have you consider sliding the whole tableview up?
edit: I did find something that looks like it does what you're looking for: Code here.
This code shrinks the inset of the table view from the bottom, which then allows it display one of those lower cells without snapping back down.
Upvotes: 2