Reference от: Using Auto Layout in UITableView for dynamic cell layouts & variable row heights
Вам необходимо подсчитайте высоту ячейки после того, как автомакет выполнит свою часть, я имею в виду, после того, как вы создали ширину/высоту внутреннего вида на основе предоставленного текста.
Следовать решение:
Первый добавить UITableView
метод делегата ниже вид контроллера,
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewAutomaticDimension;
}
высота
Вычислить ячейки в методе heightForRowAtIndexPath:
делегата:
* ЭТО ВАЖНО * : Помните, что присваивать значениям ярлыка ячеек и textView одинаковыми, как в cellForRowAtIndexPath
del egate.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellIdentifier = [NSString stringWithFormat:@"CustomCell%d%d",(int)indexPath.section, (int)indexPath.row];
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
// Set cell label text same like you did in `cellForRowAtIndexPath`
// For example
NSString *name = [self.dataArray objectAtIndex:indexPath.row];
cell.nameLabel.text = name;
cell.textView.text = @"This is long text.....";
[cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];
cell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(tableView.bounds), CGRectGetHeight(cell.bounds));
[cell setNeedsLayout];
[cell layoutIfNeeded];
CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
height += 5;
return height;
}
Теперь, если вы создали CustomCell
добавить ниже ограничений в initWithStyle:
метод клетки
// Other controls initialisation and creation code
// ....
// TextView control setup
[self.textView setTranslatesAutoresizingMaskIntoConstraints:NO];
// Above line is very important otherwise below constraint wont work
// Because by default it uses auto size constraints
// Other controls constraints
// ....
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[textView]-10-|" options:0 metrics:nil views:@{@"textView" : self.textView}]];
NSLayoutConstraint *pinToBottom = [NSLayoutConstraint constraintWithItem:self.textView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.contentView
attribute:NSLayoutAttributeBottom
multiplier:1
constant:0];
[self.contentView addConstraint:pinToBottom];
И добавить ниже метод делегата
- (void)layoutSubviews {
[super layoutSubviews];
[self.contentView setNeedsLayout];
[self.contentView layoutIfNeeded];
self.textView.preferredMaxLayoutWidth = CGRectGetWidth(self.textView.frame);
}
Но если вы еще не создали пользовательскую ячейку, и вы используя ячейку из Storyboard, добавьте нижнее ограничение в ваш superView (представление содержимого ячейки).
Вам необходимо самостоятельно изменение размеров ячеек. Посмотрите на этот учебник https://www.appcoda.com/self-sizing-cells/ – vishnuvarthan
Нет, я знаю об этом трюке. Я пытаюсь изменить ячейку представления таблицы после настройки вида таблицы. –
Не могли бы вы опубликовать код того, что вы пробовали до сих пор? –