UITextView setEditing:YES控制TextView滚动?

时间:2010-11-07 04:18:21

标签: iphone uitableview uitextview

我有一个UITableViewCell,它有一个集成的UITextView。目标是创建一个在编辑时自动扩展的单元格。现在的问题是,当UITableViewController发送setEditing:YES时,UITextView会滚动并剪辑顶部的一些文本。

我确信有更好的方法可以做到这一点,但我只是不知道如何......

#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) {
        [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

然后Table视图实现了一个委托方法:

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

有什么想法吗?我做错了吗?

1 个答案:

答案 0 :(得分:0)

事实证明,UIEdgeInsets与我的textview结合不够高。我的错!