tanvi2009
tanvi2009

Reputation: 61

NSTableCellView dynamic height with NSTextField multiline autolayout

I have a view-based NSTableView and NSTableCellView subclass which having multiline NSTextField with word-wrap enabled. Now I want my table to have dynamic row height according to textfield content. Autolayout is also enabled.

Textfield's constraints is shown in image:

contstraints

Tableview heightForRow method code :

- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row {

    VerseTableCell *cell = [self getTableCellForRow:row];
    [cell.txtSuraDetail invalidateIntrinsicContentSize];
    [cell invalidateIntrinsicContentSize];
    [cell setTranslatesAutoresizingMaskIntoConstraints:YES];

    [cell layout];
    [cell setNeedsDisplay:YES];
    [cell setNeedsUpdateConstraints:YES];
    [cell setNeedsLayout:YES];
    [cell layoutSubtreeIfNeeded];

    NSRect size = cell.txtSuraDetail.frame;

    CGFloat height = (size.size.height) + 30;

    return height;
   }

Cell Layout Method :

- (void)layout {
    [super layout];
    self.txtSuraDetail.preferredMaxLayoutWidth = self.txtSuraDetail.frame.size.width;
}

I've tried the following links but nothing helped, textfield's text is still cutting.

Upvotes: 6

Views: 2302

Answers (0)

Related Questions