【发布时间】:2015-01-28 02:09:13
【问题描述】:
我有一个静态 UITableViewCell,其中包含一个 UITextView(根据其拥有的内容动态调整大小)和一个 UIView。
单元格的高度会根据 textView 和视图的高度动态变化。
这是我的代码:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
[self textViewFitToContent:textView];
[self.tableView beginUpdates];
[self.tableView endUpdates];
[self scrollToLocation:textView];
return YES;
}
- (void)textViewFitToContent:(UITextView *)textView
{
textView.scrollEnabled = YES;
CGFloat fixedWidth = textView.frame.size.width;
CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
CGRect newFrame = textView.frame;
newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
textView.frame = newFrame;
}
- (void)scrollToLocation: (UITextView *)textView
{
UIView *parentView = textView.superview;
while (![parentView isKindOfClass:[UITableViewCell class]]) {
parentView = parentView.superview;
}
NSIndexPath *indexPath = [myTableView indexPathForCell:(UITableViewCell *)parentView];
[myTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
int padding = 48;
if (indexPath.section == 0 && indexPath.row == 0)
{
return 163;
}
else if (indexPath.section == 1 && indexPath.row == 0) {
return textView1.frame.size.height + self.view1.frame.size.height + padding;
}
else if (indexPath.section == 1 && indexPath.row == 1) {
return textView2.frame.size.height + self.view2.frame.size.height + padding;
}
else if (indexPath.section == 1 && indexPath.row == 2) {
return textView3.frame.size.height + self.view3.frame.size.height + padding;
}
else if (indexPath.section == 1 && indexPath.row == 3){
return textView4.frame.size.height + self.view4.frame.size.height + padding;
}
return 44;
}
问题
问题是,当我输入第一个 textView(我输入的每个字母都会发生这种情况)时,所有其他 textView 的高度都会变小。如果我输入任何其他 textViews,(不是第一个,)第一个 textViews 高度变大,所有其他 textViews 高度变小。
【问题讨论】:
标签: objective-c uitableview uitextview