0

What's the best way to have NSTableView cells with multiple lines ? Let's say 5 or 6 ?

I am aware of this question and the answer "iOS: UITableView cells with multiple lines?" but the mechanism seems to be different on OSX.

Community
  • 1
  • 1
Colas
  • 3,473
  • 4
  • 29
  • 68
  • Can you be more specific about the problem you're having? If you use a view-based NSTableView, you can make a NSTableCellViews with arbitrary arrangements of any kind of subviews you like in Interface Builder, very similarly to what you can do on iOS. That includes multi-line text fields, or multiple single line text fields arranged in rows. – Andrew Madsen Sep 05 '13 at 22:34
  • I wanted to avoid the pain to use view-based tableviews... – Colas Sep 06 '13 at 04:45
  • View-based table views are *much* easier to customize than cell-based table views. You're not avoiding pain by using a cell-based table view... – Andrew Madsen Sep 06 '13 at 17:33

1 Answers1

1

Based on this answer View-based NSTableView with rows that have dynamic heights :

In the delegate of your NSTableView :

@interface MenuDelegate ()

@property (nonatomic, weak) NSArrayController * theArrayController ;

@property (nonatomic, strong) NSTextFieldCell * anExtraTextFieldCell ;
@property (nonatomic) NSRect tallRect ;

@end



@implementation  MenuDelegate



#pragma mark - Initialisations


- (id)initwithArrayController:(NSArrayController *)theArrayController;
{
    self = [super init] ;

    if (self)
    {
        self.theArrayController = theArrayController ;

        self.anExtraTextFieldCell = [[NSTextFieldCell alloc] init] ;
    }

    return self ;
}



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

    if (!self.tallRect.size.width)
    {
        NSTableColumn * firstColum = tableView.tableColumns[0] ;
        self.tallRect = NSMakeRect(0, 0, firstColum.width, CGFLOAT_MAX);
    }

    // Access the content of the cell.
    NSString * content = [self.theArrayController.arrangedObjects[row] valueForKey:@"title"] ;
    self.anExtraTextFieldCell.stringValue = content ;

    CGFloat result = [self.anExtraTextFieldCell cellSizeForBounds:self.tallRect].height + 5;


    if (result < [tableView rowHeight])
    {
        result = [tableView rowHeight] ;
    }

    return result ;
}
Community
  • 1
  • 1
Colas
  • 3,473
  • 4
  • 29
  • 68