У меня есть статический UITableViewCell, содержащий UITextView (который динамически изменяется на основе его содержимого) и UIView.Динамическое изменение размера UITextView
Высота ячейки динамически изменяется в зависимости от высоты текста и вида.
Вот мой код:
- (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 становятся меньше.
Как это помогает? –
@MikeRally ваши методы делегирования ... когда вы что-то делаете для «textView», вы делаете что-то со всеми текстовыми элементами. Если вы укажете, какой textView вы хотите (по тегу), вы получите один textView для изменения за раз. – kygcoleman
Я просто сделал if (textView == textView1) {...} для каждого textView, и то же самое произошло –