首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSTableView不保留NSTableCellView

NSTableView不保留NSTableCellView
EN

Stack Overflow用户
提问于 2018-05-11 14:09:39
回答 1查看 234关注 0票数 0

这是一个非常简单的NSTableView,包含1列和由NSTableCellViews组成的行,这些列和行显示一个简单的字符串,我正在以编程方式设置该字符串(没有nib/接口生成器)。

,我无法让表视图显示字符串,也不知道为什么?!当我在表显示后捕获行选择事件时,单元格对象是nil?!

我一定是在做一些不可思议的蠢事,但我看不见。有什么想法吗?

代码语言:javascript
复制
@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

这就是登录到控制台的内容。我已经在这个问题上闲逛了好几个小时了,却什么也没得到:

代码语言:javascript
复制
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)

我得到的是:

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-12 17:40:32

我不知道到底是什么问题。我找到了几种替代的解决方案:

  1. 可以简单地从NSTextField返回tableView:viewForTableColumn:row:
  2. 还可以将NSTextField作为subView添加到NSTableCellView中,但这样做可以减少对布局的控制。
  3. 可以基于NSView创建任何自定义控件。

我选择了最后一个,并创建了一个自定义NSView,它的外观和行为都与我想要的完全一致。我就是这么做的。

代码语言:javascript
复制
@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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50294240

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档