这是一个非常简单的NSTableView,包含1列和由NSTableCellViews组成的行,这些列和行显示一个简单的字符串,我正在以编程方式设置该字符串(没有nib/接口生成器)。
,我无法让表视图显示字符串,也不知道为什么?!当我在表显示后捕获行选择事件时,单元格对象是nil?!。
我一定是在做一些不可思议的蠢事,但我看不见。有什么想法吗?
@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)我得到的是:

发布于 2018-05-12 17:40:32
我不知道到底是什么问题。我找到了几种替代的解决方案:
tableView:viewForTableColumn:row:NSTableCellView中,但这样做可以减少对布局的控制。我选择了最后一个,并创建了一个自定义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;
}
@endhttps://stackoverflow.com/questions/50294240
复制相似问题