0

I have a table view with text views in each cell. How can I scroll the tableview so that the text being entered into a cell's textview is always visible? This is different from a tableview with textfields, because a textfield does not change it's height, while textview does, if a new line is entered.

Thanks!

Balázs Vincze
  • 3,550
  • 5
  • 29
  • 60

2 Answers2

0

The UITextView changes its height each time return is pressed. Detect when return is pressed and scroll the UITableView slightly upwards.

- (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;
}
Kamil
  • 968
  • 1
  • 8
  • 18
0
  1. Declare new variable UITextView *activeTextView;

  2. In ViewDidLoad method, register the keyboard notification while show/hide.

    [self keyboardNotifications];
    
  3. Add the following method. - (void)keyboardNotifications { // Register notification when the keyboard will be show [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

        // Register notification when the keyboard will be hide
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillHide:)
                                                     name:UIKeyboardWillHideNotification
                                                   object:nil];
    }
    
  4. Set UITextview tag using indexpath in cellForRowAtIndexPath.

     #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 delegate set as activeTextView with current 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];
    }
    
Vignesh Kumar
  • 598
  • 4
  • 11