Referenz von: Using Auto Layout in UITableView for dynamic cell layouts & variable row heights
Sie müssen Berechne die Höhe der Zelle nach dem Auto-Layout. Teile meinen ich nach dem Anordnen der inneren Ansichten Breite/Höhe basierend auf dem bereitgestellten Text.
Folgen Sie der Lösung:
Erste hinzufügen UITableView
Delegatmethode unten in View-Controller,
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewAutomaticDimension;
}
berechnen Zelle Höhe in heightForRowAtIndexPath:
Delegatmethode:
* Dies ist wichtig, * : Denken Sie daran, dass Sie die Werte der Zelle label und textView genauso zuweisen wie in cellForRowAtIndexPath
del Egate-Methode.
- (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;
}
Nun, wenn Sie CustomCell
unter Einschränkungen in initWithStyle:
Zelle Methode hinzufügen erstellt haben
// 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];
Und fügen Sie unter Delegatmethode
- (void)layoutSubviews {
[super layoutSubviews];
[self.contentView setNeedsLayout];
[self.contentView layoutIfNeeded];
self.textView.preferredMaxLayoutWidth = CGRectGetWidth(self.textView.frame);
}
Aber wenn Sie keine benutzerdefinierten Zelle erstellt haben und Sie Wenn Sie eine Zelle aus dem Storyboard verwenden, fügen Sie der superView (Inhaltsansicht der Zelle) die bottom constraint hinzu.
Sie brauchen Selbst Zellen Ändern der Größe. Schauen Sie in dieses Tutorial https://www.appcoda.com/self-sizing-cells/ – vishnuvarthan
Nein, ich weiß über diesen Trick. Ich versuche, nach dem Einrichten der Tabellenansicht die Tabellenansichtszelle zu ändern. –
Könnten Sie den Code von dem, was Sie bisher versucht haben, posten? –