Force UITableView to scroll to top?

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

  • table view is not loading data
  • TableViewCell subviews can't access in IOS 7
  • How do i automatically scroll in a table view? (Swift)
  • How do you make a UITextView detect link part in the text and still have userInteractionDisabled?
  • UITableView with controller separate from ViewController
  • How can I create different amount of uilabel in each uitableview row with uitableviewcell?
  • 6 Solutions Collect From Internet About “Force UITableView to scroll to top?”

    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.

    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];
    

    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.

    UITableView is a subclass of UIScrollView, so you can also use:

    [mainTableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];
    

    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

    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];