質問

I have a UITableView cell with dynamically growing UITextView. The height of the cell is getting set correctly. However, for some reason auto-scroll doesn't adjust the offset when I hit Enter and begin a new line (see image A) but only when I type the first character on that line (see image B). Any thoughts on this?

EDIT:

I'm using HPGrowingTextView and taking the height that its delegate gives me. It fills the entire cell. I measured the screenshot against the value it gives me and it matched. Below are the pertinent methods:

-(void)growingTextView:(HPGrowingTextView *)growingTextView willChangeHeight:(float)height {
    _messageHeight = height;

    // this will trigger cell height resize
    [self.tableView beginUpdates];
    [self.tableView endUpdates];

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == kMessageRow) {
        return _messageHeight;
    }

    return 50;
}

Image A: Cursor is partially hidden when starting a new line A: Cursor is partially hidden when starting a new line

Image B: Now auto-scroll makes the adjustment once first character is received B: Now auto-scroll makes the adjustment once first character is received

役に立ちましたか?

解決

The issue wasn't with the growing text view but rather with the UITableView auto-scroll which doesn't seem to fire when detecting the return key. So my solution was to check for the new line character manually and to force a scroll to the bottom of the cell.

- (void)growingTextViewDidChange:(HPGrowingTextView *)growingTextView {

    NSString *text = growingTextView.text;
    NSInteger endIndex = text.length - 1;

    if (endIndex >= 0 && [[text substringFromIndex:endIndex] isEqualToString:@"\n"]) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:textViewSection];
        [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:NO];
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top