2016-09-25 73 views
0

我有一个表格视图在每个单元格中的文字视图。如何滚动tableview,以便输入到单元格textview中的文本始终可见?这与带有文本字段的tableview不同,因为如果输入新行,textfield不会更改其高度,而textview会更改它。键盘避免tableview与textviews

谢谢!

回答

0

UITextView每次按下回车键都会改变其高度。按下返回时检测并略微向上滚动UITableView

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { 

    if([text isEqualToString:@"\n"]) { 

     CGPoint currentOffset=myTableView.contentOffset; 
     currentOffset.y-=10; 
     [myTableview setContentOffset:currentOffset]; 

    } 

    return YES; 
} 
+0

但是,如果用户不按什么样的回报?文本被简单包装到下一行 –

+0

@BalázsVincze检查了这一点http://stackoverflow.com/questions/20008713/detect-moment-when-newline-starts-in-uitextview – Kamil

0
  1. 声明新的变量 的UITextView * activeTextView;

  2. 在ViewDidLoad方法中,在显示/隐藏时注册键盘通知。

    [self keyboardNotifications]; 
    
  3. 添加以下方法。 - (无效)keyboardNotifications { //注册通知时,键盘将显示 [[NSNotificationCenter defaultCenter]的addObserver:自 选择器:@selector(keyboardWillShow :) 名:UIKeyboardWillShowNotification 对象:无];

    // Register notification when the keyboard will be hide 
        [[NSNotificationCenter defaultCenter] addObserver:self 
                  selector:@selector(keyboardWillHide:) 
                   name:UIKeyboardWillHideNotification 
                   object:nil]; 
    } 
    
  4. 在cellForRowAtIndexPath中使用indexpath设置UITextview标记。

    #pragma mark - Keyboard handling 
    -(void) keyboardWillShow:(NSNotification *)note { 
        if(activeTextView) 
         if ([TableCell count] < activeTextView.tag) { // It is validate table cell count and activeTextView.tag 
          NSDictionary* info = [note userInfo]; 
          CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 
    
          UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0); 
          tableV.contentInset = contentInsets; 
          tableV.scrollIndicatorInsets = contentInsets; 
          [tableV scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:activeTextView.tag inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES]; 
         } 
        } 
    
    } 
    
    -(void) keyboardWillHide:(NSNotification *)note { 
        [UIView animateWithDuration:.3 animations:^(void) { 
         tableV.contentInset = UIEdgeInsetsZero; 
         tableV.scrollIndicatorInsets = UIEdgeInsetsZero; 
        }]; 
    } 
    
  5. UITextView的代表设定为activeTextView与当前的UITextView。

    #pragma mark - UITextViewDelegate :: 
    
    - (BOOL)textViewShouldBeginEditing:(UITextView *)textView 
    { 
        return YES; 
    } 
    
    - (BOOL)textViewShouldEndEditing:(UITextView *)textView 
    { 
        return YES; 
    } 
    
    // To be link with your TextView event "Editing Did Begin" 
    // memoryze the current TextView 
    - (void)textViewDidBeginEditing:(UITextView *)textView 
    { 
        activeTextView = textView; 
        [textView becomeFirstResponder]; 
    } 
    
    // To be link with your TextView event "Editing Did End" 
    // release current TextView 
    - (void)textViewDidEndEditing:(UITextView *)textView 
    { 
        activeTextView = nil; 
        [textView resignFirstResponder]; 
    }