带有 NSComboBox 的自定义数据源不显示任何内容
您好,我在尝试在 NSComboBox 中设置数据源时遇到以下问题。
这是我的自定义数据源类:
@interface CComboDatasource : NSObject <NSComboBoxDataSource> {
@private
NSMutableArray* values;
}
@property (nonatomic,retain) NSMutableArray* values;
-(int)itemCount;
@end
@implementation CComboDatasource
@synthesize values;
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
values=[[NSMutableArray alloc] init];
[values addObject:@"A"];
[values addObject:@"B"];
[values addObject:@"C"];
}
return self;
}
- (NSInteger)numberOfItemsInComboBox:(NSComboBox *)aComboBox
{
return [values count];
}
- (id)comboBox:(NSComboBox *)aComboBox objectValueForItemAtIndex:(NSInteger)index
{
return [values objectAtIndex:index];
}
- (void)dealloc
{
[values release];
[super dealloc];
}
@end
稍后在另一个文件中,我将我的 IBOutlet
与我的 NSComboBox
对象 (c_box) 连接,并设置数据源 (CComboDatasource* data_source)< /代码>。
[c_box setUsesDataSource:TRUE];
[c_box setDataSource:data_source];
[c_box setEditable:NO];
在执行前面的操作后,组合框中没有显示任何内容,我做错了什么?
Greetings I have the following problem trying to set a datasource in an NSComboBox
.
This is my custom datasource class:
@interface CComboDatasource : NSObject <NSComboBoxDataSource> {
@private
NSMutableArray* values;
}
@property (nonatomic,retain) NSMutableArray* values;
-(int)itemCount;
@end
@implementation CComboDatasource
@synthesize values;
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
values=[[NSMutableArray alloc] init];
[values addObject:@"A"];
[values addObject:@"B"];
[values addObject:@"C"];
}
return self;
}
- (NSInteger)numberOfItemsInComboBox:(NSComboBox *)aComboBox
{
return [values count];
}
- (id)comboBox:(NSComboBox *)aComboBox objectValueForItemAtIndex:(NSInteger)index
{
return [values objectAtIndex:index];
}
- (void)dealloc
{
[values release];
[super dealloc];
}
@end
Later in another file I connect my IBOutlet
with my NSComboBox
object (c_box) and I set the datasource (CComboDatasource* data_source)
.
[c_box setUsesDataSource:TRUE];
[c_box setDataSource:data_source];
[c_box setEditable:NO];
After the previous actions nothing is displayed in the combo box, what am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你所拥有的对我来说基本上是正确的。我可以想到一些你可以尝试的事情:
1)尝试暂时将“return [values count]”替换为“return 5”,并将“return [values objectAtIndex:index]”替换为“return @”任意字符串“”。如果“任意字符串”出现在组合框中,您就会知道问题出在“值”数组上。
2)尝试像这样初始化“values”数组:(
这是 NSArray 中提供的一种便捷方法。)
如果您坚持使用 alloc-init 方法,则应该以这种方式创建一个单独的临时数组,将其分配给“values”,然后释放它。否则,由于您已使用“保留”对“值”进行了属性化,因此您将保留它两次。
3) 尝试在 c_box 调用末尾添加此行:
并且每当您更改数据源数组时,请再次调用此行。
4)我不明白为什么将数据源类与控制组合框的类分开应该是一个问题,但如果它仍然不起作用,请尝试使拥有组合框出口的窗口/视图控制器成为实现 NSComboBoxDataSource 协议的类(numberOfItemsIn 和 objectValueFor 方法),并将“值”放入此控制器类中,或者授予此类访问“值”的权限。
希望有帮助。
What you have looks basically right to me. I can think of a few things you could try:
1) Try temporarily replacing "return [values count]" with "return 5" and replacing "return [values objectAtIndex:index]" with "return @"arbitraryString"". If "arbitraryString" then shows up in the combobox, you'll know the problem is with the "values" array.
2) Try initializing the "values" array like this:
(It's a convenience method offered in NSArray.)
If you stick with an alloc-init method, you should make a separate temporary array that way, assign it to "values," then release it. Otherwise, since you've propertized "values" with "retain," you're retaining it twice.
3) Try adding this line at the end of your c_box calls:
And any time you change the data source array, call this again.
4) I don't see why separating the data source class from the class controlling the combobox should be a problem, but if it's still not working, try making the window/view controller that owns the combobox outlet the class that implements the NSComboBoxDataSource protocol (the numberOfItemsIn and objectValueFor methods), and either put "values" in this controller class or give this class access to "values."
Hope that helps.
好吧,我发现了问题,为了让自定义数据源类正常工作,您需要
Ok I found the problem ,in order by the custom datasource class to work u need
我对组合框有同样的问题:objectValueForItemAtIndex:因为我有 10 个组合框,每个组合框我都通过以下方式检查:
if (aComboBox == _myCombo)
8 组合框工作正常,但 2 则不行。我不知道自己做错了什么,也不知道为什么别人能成功。经过大约两周的思考这个问题,我找到了解决方案。
解决方案是在从 nib 唤醒中选择选项之前重新加载数据。
I had the same problem with comboBox:objectValueForItemAtIndex: because I have 10 combo box, every combo box I checking by:
if (aComboBox == _myCombo)
8 combo box worked fine, but 2 did not. I didn't know what I was doing wrong and why others worked. After thinking about this problem about 2 weeks, I found a solution.
The solution is to reloadData before select option in awake from nib.