> I’m still not out of the wood yet though.
Sorry. I should have provided more details.
What I do to support table cell view wrapping is this. However, I have to
support back to 10.9 so it may be possible to use some of the newer auto
NSTextField line wrapping stuff.
1. Create a nib containing a wrapping cell view and associate it with a column
that we want to wrap the content of. View based tableviews use Auto Layout for
interior layout but in IB it doesn’t generally add and constrains to the
default table cell views. So make a BPWrappingTableCellView nib that contains
an NSTextField configured to wrap and constrained to the width and height of
the cell.
columnCellNib = [[NSNib alloc] initWithNibNamed:@"BPWrappingTableCellView"
bundle:nil];
[self.tableView registerNib:columnCellNib forIdentifier:@"action”];
2. Now the height of a row in the tableview must equal the height of the
tallest cell in the row. There is a NSTableViewDelegate call for this. IN this
example I have only one column that I need to wrap. If you have more you will
need to find out which column has the tallest content.
- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row
{
// calculate height of row based on height of cell view in variable height
column
id obj = self.arrayController.arrangedObjects[row];
CGFloat height = [tableView bp_heightOfRowForString:obj.description
variableHeightColumnIdentifier:@"action"];
return height;
}
3. Calculate height of cell using a worker cell. This is an NSTableView
category method.
#pragma mark -
#pragma mark Row height support
- (CGFloat)bp_heightOfRowForString:(NSString *)text
variableHeightColumnIdentifier:(NSString *)identifier
{
/*
Calculate height of row to accomodate wrapping text field in the
description column.
On 10.11 it might be possible to just use the fields Automatic setting.
http://stackoverflow.com/questions/7504546/view-based-nstableview-with-rows-that-have-dynamic-heights
*/
// we use a layout worker cell for height calcs.
if (!self.layoutCellView) {
self.layoutCellView = [self makeViewWithIdentifier:identifier
owner:nil];
}
// reset size
CGFloat width = [self tableColumnWithIdentifier:identifier].width;
[self.layoutCellView setFrameSize:NSMakeSize(width, 10)];
// set the cell text
self.layoutCellView.textField.stringValue = text;
// layout to calculate size
self.layoutCellView.textField.preferredMaxLayoutWidth = width -
self.layoutCellView.textField.frame.origin.x - 10;
[self.layoutCellView layoutSubtreeIfNeeded];
CGFloat height = self.layoutCellView.frame.size.height;
if (height < 30) height = 30;
if (height > 150) height = 150;
return height;
}
4. Deal with column resizing:
- (void)tableViewColumnDidResize:(NSNotification *)aNotification
{
// notify table view of row height change if variable height column width
changes
NSTableColumn *tableColumn = aNotification.userInfo[@"NSTableColumn"];
NSTableView *tableView = tableColumn.tableView;
[tableView bp_columnDidResize:tableColumn
variableHeightColumnIdentifiers:@[@"action"]];
}
Another NSTableView category method.
- (void)bp_columnDidResize:(NSTableColumn *)tableColumn
variableHeightColumnIdentifiers:(NSArray *)columnIds
{
/*
Trigger row height recalculation when change table column width
*/
if (self.numberOfRows > 0 && [columnIds
containsObject:tableColumn.identifier]) {
NSIndexSet *rowIndexSet = [NSIndexSet
indexSetWithIndexesInRange:NSMakeRange(0, self.numberOfRows)];
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:0];
[self noteHeightOfRowsWithIndexesChanged:rowIndexSet];
[NSAnimationContext endGrouping];
}
}
HTH
J
_______________________________________________
Cocoa-dev mailing list ([email protected])
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]