NSTableView不保留NSTableCellView

时间:2018-05-11 14:09:40

标签: objective-c cocoa nstableview

这是一个非常简单的NSTableView,其中包含1列和包含NSTableCellViews的行,它们显示一个简单的字符串,我以编程方式设置它(没有nib / interface builder)。

我无法让tableview显示字符串而无法找出原因?!当我在显示表格后捕获行选择事件时,单元格对象为零?!

我必须做一些非常愚蠢的事情,但我无法看到它。有什么想法吗?

@implementation PreferencesSelectorPanel
-(id)initWithWidth:(CGFloat)width
{
    self = [super init];
    if(self) {
        self.translatesAutoresizingMaskIntoConstraints = NO;
        self.autoresizesSubviews = YES;

        _preferenceTypes = @[@"Settings 1", @"Settings 2", @"Settings 3", @"Settings 4", @"Settings 5"];

        _tv = [NSTableView new];
        _tv.translatesAutoresizingMaskIntoConstraints = NO;
        _tv.autoresizesSubviews = YES;
        _tv.focusRingType = NSFocusRingTypeNone;
        _tv.delegate = self;
        _tv.dataSource = self;
        _tv.rowHeight = 40;
        _tv.headerView = nil;
        _tv.selectionHighlightStyle = NSTableViewSelectionHighlightStyleRegular;
        _tv.allowsColumnReordering = NO;
        _tv.allowsColumnResizing = NO;
        _tv.allowsEmptySelection = NO;
        _tv.allowsTypeSelect = NO;
        _tv.gridStyleMask = NSTableViewSolidHorizontalGridLineMask;//NSTableViewGridNone;

        NSTableColumn *col1 = [[NSTableColumn alloc] initWithIdentifier:@"c1"];
        col1.resizingMask = NSTableColumnAutoresizingMask;
        [_tv addTableColumn:col1];

        [self addSubview:_tv];

        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(10)-[_tv(200)]-(>=10)-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_tv)]];
        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(10)-[_tv]-(10)-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_tv)]];
    }
    return self;
}

-(NSInteger)numberOfRowsInTableView:(NSTableView *)tv
{
    NSLog(@"numberOfRowsInTableView");
    return _preferenceTypes.count;
}

-(NSView *)tableView:(NSTableView *)tv viewForTableColumn:(NSTableColumn *)tc row:(NSInteger)row
{
    NSTextField *tf = [NSTextField 
    labelWithString:_preferenceTypes[row]];
    tf.textColor = [NSColor blackColor];

    NSTableCellView *cell = [NSTableCellView new];
    cell.textField = tf;

    NSLog(@"Row for row %d with label:%@",(int)row, cell.textField.stringValue);
    return cell;
}

-(void)tableViewSelectionDidChange:(NSNotification *)notification
{
    NSTableCellView *cell = _tv.selectedCell;
    NSLog(@"%@", cell.textField.stringValue);
}
@end

这是记录到控制台的内容。我已经在这里闲逛了好几个小时而且没有在哪里:

2018-05-11 numberOfRowsInTableView
2018-05-11 Row for row 0 with label:Settings 1
2018-05-11 Row for row 1 with label:Settings 2
2018-05-11 Row for row 2 with label:Settings 3
2018-05-11 Row for row 3 with label:Settings 4
2018-05-11 Row for row 4 with label:Settings 5
2018-05-11 (null)
2018-05-11 (null)
2018-05-11 (null)

这就是我得到的: enter image description here

1 个答案:

答案 0 :(得分:0)

我无法弄清楚究竟是什么问题。我找到了几种替代解决方案:

  1. 可以从tableView:viewForTableColumn:row:
  2. 返回NSTextField
  3. 也可以将NSTextField作为子视图添加到NSTableCellView,但这样做可以减少对布局的控制。
  4. 可以基于NSView创建任何自定义控件。
  5. 我选择了最后一个并创建了一个自定义的NSView,其外观和行为完全像我想要的那样。这就是我所做的。

    @interface PreferenceTableViewCell : NSView
    @property (nonnull, strong, readonly) NSTextField *tf;
    @end
    
    @implementation PreferenceTableViewCell
    -(id)init
    {
        self = [super init];
        if(self) {
            self.translatesAutoresizingMaskIntoConstraints = NO;
            self.autoresizesSubviews = YES;
            _tf = [NSTextField labelWithString:@""];
            _tf.translatesAutoresizingMaskIntoConstraints = NO;
            _tf.autoresizesSubviews = YES;
            [self addSubview:_tf];
            [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(10)-[_tf]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_tf)]];
            [self addConstraint:[NSLayoutConstraint constraintWithItem:_tf attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];
        }
        return self;
     }
     @end