简体   繁体   中英

how to set UITableView's scroll position to previous Position in ios

When Click on textfield in cell, then open key pad and scroll tableview.

textFieldShouldReturn: call then tableview set Previous Position. How can I do this?

My Tableview Scroll Code

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
  CGPoint point = [textField.superview convertPoint:textField.frame.origin toView:tblemailconfiguration];
  CGPoint contentOffset = tblemailconfiguration.contentOffset;
  contentOffset.y += (point.y - contentOffset.y - self.navigationController.navigationBar.frame.size.height); // Adjust this value as you need
  [tblemailconfiguration setContentOffset:contentOffset animated:YES];

  return YES;
}

Just do like below:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    CGPoint point = [textField.superview convertPoint:textField.frame.origin toView:tblemailconfiguration];
    CGPoint contentOffset = tblemailconfiguration.contentOffset;
    // Record the tableview's content offset when editing begin
    lastContentOffset = contentOffset;
    contentOffset.y += (point.y - contentOffset.y - self.navigationController.navigationBar.frame.size.height); // Adjust this value as you need
    [tblemailconfiguration setContentOffset:contentOffset animated:YES];

    return YES;
}


- (void)textFieldDidEndEditing:(UITextField *)textField {
    // When editing reach end, just set tableview's content offset to last. 
    [tblemailconfiguration setContentOffset:lastContentOffset animated:YES];
}

Add Observers in viewWillAppear

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardDidHideNotification object:nil];

Remove Observer in viewWillDisappear

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];

Now add this methods in your view controller

 #pragma mark - keyboard Hide Show



 -(void) keyboardWasShown:(NSNotification *)notification
    {
        //Manages scrollview content on keyboard hide show
         NSDictionary *info = [notification userInfo];
         NSValue *kbFrame = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
         CGRect keyboardFrame = [kbFrame CGRectValue];
        CGFloat height = keyboardFrame.size.height;

        [_scrollView setContentInset:UIEdgeInsetsMake(0, 0, height, 0)];
        [_scrollView setScrollIndicatorInsets:UIEdgeInsetsMake(0, 0, height, 0)];

     }
    - (void) keyboardWillBeHidden:(NSNotification *)notification
    {
        //Manages scrollview content on keyboard hide show
        UIEdgeInsets contentInsets = UIEdgeInsetsZero;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.3f];
        [_scrollView setContentInset:UIEdgeInsetsMake(0, 0, 0, 0)];
        [_scrollView setScrollIndicatorInsets:UIEdgeInsetsMake(0, 0, 0, 0)];


        [UIView commitAnimations];
    }

    #pragma mark -  Textfield Delegate Methods -

    - (void)textFieldDidBeginEditing:(UITextField *)textField
    {

        CGRect r = [textField convertRect:textField.frame toView:_scrollView];
        [self.scrollView scrollRectToVisible:r animated:YES];
    }

我建议在打开键盘之前记录indexPath,然后使用UITableView:scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animatedtextFieldShouldReturn: UITableView:scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated textFieldShouldReturn:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM