相当于 XCode 中的“ListBox”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6338541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:15:48  来源:igfitidea点击:

Equivalent to a "ListBox" in XCode?

objective-cxcodecocoa

提问by Voldemort

You know Visual Studio, that awesome element called "ListBox"? Just a box that would list a bunch of strings.

你知道 Visual Studio,那个叫做“ListBox”的很棒的元素吗?只是一个会列出一堆字符串的框。

I am now working with XCode, and I found this class in the interface builder "NSScrollView". It seems to be able to list me a couple strings. It says it got a NSTextView inside, but, how do I access it?

我现在正在使用 XCode,我在界面构建器“NSScrollView”中找到了这个类。它似乎能够给我列出几个字符串。它说它里面有一个 NSTextView,但是,我如何访问它?

I am not even sure if NSScrollView is the correct solution I need, but if I could simply access the NSTextView inside it, I think it would be enough.

我什至不确定 NSScrollView 是否是我需要的正确解决方案,但如果我可以简单地访问其中的 NSTextView,我认为就足够了。

采纳答案by Joshua Nozzi

See NSTableView.

参见NSTableView

As for getting to a text view inside a scroll view, create an Interface Builder outlet (IBOutlet) and connect it to the text view itself, rather than the scroll view.

至于在滚动视图中访问文本视图,请创建一个 Interface Builder 插座 (IBOutlet) 并将其连接到文本视图本身,而不是滚动视图。

回答by alban

To get to a text view inside a scroll view; you need to select the controller with your outlet defined; click and hold control and then drag the blue connection line from your controller to the top line of the scroll view; then just wait for a blue line to appear; this will then prompt to let you link your outlet to the text view.

进入滚动视图内的文本视图;您需要选择定义了插座的控制器;单击并按住 control,然后将蓝色连接线从控制器拖动到滚动视图的顶行;然后等待蓝线出现;这将提示您将插座链接到文本视图。

回答by Cliff Ribaudo

Josh's answer above to use NSTableView is correct. For those not that familiar with it, it can seem like a much bigger task than it actually turns out to be. Hopefully this saves people some time.

Josh 上面使用 NSTableView 的回答是正确的。对于那些不太熟悉它的人来说,这似乎是一项比实际情况要大得多的任务。希望这可以为人们节省一些时间。

Rather than fight with NSTableCellViewassumptions, you can create any type of simple view you want and use auto layout (or even return a simple NSTextView. This is what I did to get more control over layout of my text strings:

NSTableCellView您可以创建任何类型的简单视图并使用自动布局(甚至返回一个简单的 NSTextView。这是我为了更好地控制文本字符串的布局所做的),而不是与假设作斗争:

@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

Then put this whereever you need the list of strings (or controls, or whatever):

然后把它放在任何你需要字符串列表(或控件,或其他)的地方:

_tv = [NSTableView new];
_tv.translatesAutoresizingMaskIntoConstraints = NO;
_tv.autoresizesSubviews = YES;
_tv.focusRingType = NSFocusRingTypeNone;
_tv.delegate = self;
_tv.dataSource = self;
_tv.rowHeight = 40; // Use this to adjust the height of your cell or do it in cell.
_tv.headerView = nil;
_tv.selectionHighlightStyle = NSTableViewSelectionHighlightStyleRegular;
_tv.allowsColumnReordering = NO;
_tv.allowsColumnResizing = NO;
_tv.allowsEmptySelection = NO;
_tv.allowsTypeSelect = NO;
_tv.gridStyleMask = NSTableViewGridNone;
[panel addSubview:_tv];

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

Then in whatever is set as the delegate and datasource for the NSTableViewadd these methods:

然后在任何设置为NSTableView添加这些方法的委托和数据源中:

-(NSInteger)numberOfRowsInTableView:(NSTableView *)tv
{
   return stringArray.count;
}

-(NSView *)tableView:(NSTableView *)tv viewForTableColumn:(NSTableColumn *)tc row:(NSInteger)row
{
    // This can be ANY NSView based control built as shown above.
    PreferenceTableViewCell *cell = [PreferenceTableViewCell new];
    cell.tf.stringValue = stringArray[row];        
    return cell;
}

-(void)tableViewSelectionDidChange:(NSNotification *)notification
{
    // Code to do whatever when a list item is selected.
}

That is basically it for a simple list. See the Apple Docs on NSTableView for more details on how to bind the table to a data sources and more complicated problems.

这基本上就是一个简单的列表。有关如何将表绑定到数据源以及更复杂的问题的更多详细信息,请参阅 NSTableView 上的 Apple 文档。