开发者

UITextView setEditing:YES makes the control of the TextView scroll?

开发者 https://www.devze.com 2023-01-24 08:20 出处:网络
I have a UITableViewCell that has an integrated UITextView.The goal is to make a cell that auto-expands while editing.The issue right now is that when the UITableViewController sends setEditing:YES, t

I have a UITableViewCell that has an integrated UITextView. The goal is to make a cell that auto-expands while editing. The issue right now is that when the UITableViewController sends setEditing:YES, the UITextView scrolls and clips some of the text at the top.

I'm sure there is a better way to do this but I just don't know how...

#import "PLTextViewCell.h"

@implementation PLTextViewCell

@synthesize delegate=_delegate;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
        _textView = [[UITextView alloc] initWithFrame:CGRectMake(90, 0, 200, 80)];
        [_textView setEditable:NO];
        [_textView setFont:[UIFont systemFontOfSize:15.0]];
        [_textView setDelegate:self];
        [_textView setScrollEnabled:NO];
        [[self contentView] addSubview:_textView];
    }
    return self;
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [_textView setEditable:editing];
    [_textView scrollRangeToVisible:NSMakeRange(0, 1)];
    [self textViewDidChange:_textView];
}

- (void)dealloc {
    [_textView dealloc];
    [super dealloc];
}

- (void)setTextValue:(NSString *)value {
    [_textView setText:value];
    [self textViewDidChange:_textView];
}

- (NSString *)textValue {
    return [_textView text];
}

- (CGFloat)cellHeight {
    CGSize mySize = [_textView contentSize];
    NSLog(@"cell height: %f", mySize.height);
    return mySize.height;
}

#pragma mark -
#pragma mark Text view delegate

- (void)textViewDidChange:(UITextView *)textView {
    CGSize mySize = [_textView contentSize];
    if (mySize.height > self.bounds.size.height) {
       开发者_如何学Go [textView scrollRectToVisible:CGRectMake(0,textView.contentSize.height-1,1,1) animated:NO];
        if ([self delegate] != nil) {
            [[self delegate] tableViewCellDidChangeHeight:self];
        }
        [textView setFrame:CGRectMake(90, 0, mySize.width, mySize.height)];
        [self setNeedsLayout];
    }
}

@end

Then the Table view implements a delegation method:

- (void)tableViewCellDidChangeHeight:(PLTextViewCell *)cell {
    [self.tableView beginUpdates];
    [self.tableView endUpdates];
}

Any ideas? Am I doing this all wrong?


Turns out that it was the UIEdgeInset combined with my textview not being tall enough. My mistake!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号