我正在尝试调整 UITableViewCell 中的 UITextView 的大小。我也想相应地调整 UITableViewCell 的大小。我的 UITableViewCell 正确调整大小,但 UITextView 没有。它最终只有两条线,而不是正确的大小。当我在 cellForRowAtIndexPath 方法中初始化它时,它看起来像:
UITextView *notesTextView = [[UITextView alloc] initWithFrame:CGRectMake(83, 12, TEXTVIEW_WIDTH, 22)];
notesTextView.tag = TEXTVIEW_TAG;
notesTextView.delegate = self;
[cell.contentView addSubview:notesTextView];
当我尝试在 textView 委托方法中修改它时,我会:
CGSize size = [textView.text sizeWithFont:textView.font constrainedToSize:CGSizeMake(TEXTVIEW_WIDTH, 460) lineBreakMode:UILineBreakModeWordWrap];
NSLog(@"size: %@", NSStringFromCGSize(size)); // has the correct size
if (size.height > self.notesRowHeight) {
self.notesRowHeight = size.height;
UITextView *aTextView = (UITextView *)[self.view viewWithTag:TEXTVIEW_TAG];
CGRect textViewFrame = CGRectMake(aTextView.frame.origin.x, aTextView.frame.origin.y, TEXTVIEW_WIDTH, size.height);
aTextView.frame = textViewFrame;
aTextView.contentSize = size;
[aTextView sizeToFit];
[aTextView sizeThatFits:size];
NSLog(@"%@", NSStringFromCGRect(aTextView.frame)); // has the correct height
所以最终发生的是 tableView 正确调整大小,即使 textView 框架的大小正确(例如 {{83, 12}, {197, 120}}),textView 只有 2 行。它最终不会像我预期的那样占用 UITableViewCell 的大小。
编辑:当我尝试在不是 UITableViewCell 的普通 UIView 中调整 UITextView 的框架时,它工作得很好。所以我不确定在这种情况下有什么不同。