簡體   English   中英

如何在iOS中將UITableView的滾動位置設置為上一個Position

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

當單擊單元格中的文本字段時,然后打開鍵盤並滾動tableview。

textFieldShouldReturn:調用然后將tableview設置為Previous Position。 我怎樣才能做到這一點?

我的Tableview滾動代碼

- (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;
}

就像下面這樣:

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

在viewWillAppear中添加觀察者

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

刪除viewWillDisappear中的Observer

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

現在,在您的視圖控制器中添加此方法

 #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:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM