2016-07-27 67 views
0

当单击单元格中的textfield时,然后打开键盘并滚动tableview。如何将UITableView的滚动位置设置为ios中的上一个位置

textFieldShouldReturn: call then tableview set Previous Position。我怎样才能做到这一点?

我泰伯维滚动码

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

快速的解决办法是记住了'contentOffset.y'在另一个全局变量并在'textFieldShouldReturn'委托中将'tblemailconfiguration'设置为保存的contentOffset。 –

+0

提供我的代码? –

+0

好吧,我已经添加了一个答案。 –

回答

0

只要做到象下面这样:

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

Right Ans Zylenv –

+0

@AvinashVaghasiya:如果已解决问题,请勾选绿色复选标记将其标记为已接受。 –

0

添加观察员在viewWillAppear中

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

在viewWillDisappear

删除观察
[[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]; 
    } 
0

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