NSCollectionView 不绘制任何内容
我正在尝试设置一个 NSCollectionView (我过去曾成功完成此操作,但由于某种原因这次失败了)。
我有一个名为“TestModel”的模型类,它有一个仅返回字符串的 NSString
属性(目前仅用于测试目的)。然后,我在主应用程序委托类中有一个 NSMutableArray 属性声明,并向该数组添加 TestModel 对象的实例。
然后,我有一个数组控制器,其内容数组绑定了应用程序委托的 NSMutableArray 。我可以确认到目前为止一切都工作正常; NSLogging:
[[[arrayController arrangedObjects] objectAtIndex:0] teststring]
工作正常。
然后,我为集合视图设置(itemPrototype 和内容)以及集合视图项(视图)设置了所有适当的绑定。然后,我在集合项视图中有一个文本字段,该文本字段绑定到集合视图 Item.representedObject.teststring
。但是,当我启动应用程序时,集合视图中没有显示任何内容,只是一个空白的白屏。我缺少什么?
更新:这是我使用的代码(由 wil Shipley 请求):
// App delegate class
@interface AppController : NSObject {
NSMutableArray *objectArray;
}
@property (readwrite, retain) NSMutableArray *objectArray;
@end
@implementation AppController
@synthesize objectArray;
- (id)init
{
if (self = [super init]) {
objectArray = [[NSMutableArray alloc] init];
}
return self;
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
TestModel *test = [[[TestModel alloc] initWithString:@"somerandomstring"] autorelease];
if (test) [objectArray addObject:test];
}
@end
// The model class (TestModel)
@interface TestModel : NSObject {
NSString *teststring;
}
@property (readwrite, retain) NSString *teststring;
- (id)initWithString:(NSString*)customString;
@end
@implementation TestModel
@synthesize teststring;
- (id)initWithString:(NSString*)customString
{
[self setTeststring:customString];
}
- (void)dealloc
{
[teststring release];
}
@end
然后就像我说的那样,数组控制器的内容数组绑定到这个“objectArray”,并且 NSCollectionView 的内容绑定到数组控制器.arrangedObjects。我可以通过 NSLogging [arrayControllerarrangedObjects] 验证数组控制器中是否包含对象,并且它返回正确的对象。只是 NSCollectionView 中没有显示任何内容。
更新2:如果我记录[collectionView内容],我什么也得不到:
2009-10-21 08:02:42.385 CollViewTest[743:a0f] (
)
问题可能就在那里。
更新3:根据要求,这里是Xcode项目:
http://www.mediafire。 com/?mjgdzgjjfzw
它是一个菜单栏应用程序,因此没有窗口。当您构建并运行应用程序时,您将看到一个显示“test”的菜单栏项,这将打开包含 NSCollectionView 的视图。
谢谢
I'm trying to set up an NSCollectionView
(I have done this successfully in the past, but for some reason it fails this time).
I have a model class called "TestModel", and it has an NSString
property that just returns a string (just for testing purposes right now). I then have an NSMutableArray
property declaration in my main app delegate class, and to this array I add instances of the TestModel
object.
I then have an Array Controller that has its Content Array bound the app delegate's NSMutableArray
. I can confirm that everything up to here is working fine; NSLogging:
[[[arrayController arrangedObjects] objectAtIndex:0] teststring]
worked fine.
I then have all the appropriate bindings for the collection view set up, (itemPrototype and content), and for the Collection View Item (view). I then have a text field in the collection item view that is bound to Collection View Item.representedObject.teststring
. However NOTHING displays in the collection view when I start the app, just a blank white screen. What am I missing?
UPDATE: Here is the code I use (requested by wil shipley):
// App delegate class
@interface AppController : NSObject {
NSMutableArray *objectArray;
}
@property (readwrite, retain) NSMutableArray *objectArray;
@end
@implementation AppController
@synthesize objectArray;
- (id)init
{
if (self = [super init]) {
objectArray = [[NSMutableArray alloc] init];
}
return self;
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
TestModel *test = [[[TestModel alloc] initWithString:@"somerandomstring"] autorelease];
if (test) [objectArray addObject:test];
}
@end
// The model class (TestModel)
@interface TestModel : NSObject {
NSString *teststring;
}
@property (readwrite, retain) NSString *teststring;
- (id)initWithString:(NSString*)customString;
@end
@implementation TestModel
@synthesize teststring;
- (id)initWithString:(NSString*)customString
{
[self setTeststring:customString];
}
- (void)dealloc
{
[teststring release];
}
@end
And then like I said the content array of the Array Controller is bound to this "objectArray", and the Content of the NSCollectionView is bound to Array Controller.arrangedObjects. I can verify that the Array Controller has the objects in it by NSLogging [arrayController arrangedObjects], and it returns the correct object. Its just that nothing displays in the NSCollectionView.
UPDATE 2: If I log [collectionView content] I get nothing:
2009-10-21 08:02:42.385 CollViewTest[743:a0f] (
)
The problem is probably there.
UPDATE 3: As requested here is the Xcode project:
http://www.mediafire.com/?mjgdzgjjfzw
Its a menubar app, so it has no window. When you build and run the app you'll see a menubar item that says "test", this opens the view that contains the NSCollectionView.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是你没有正确使用KVC。您可以做两件事。
方法一:简单但不太优雅
[[self mutableArrayValueForKey:@"objectArray"] addObject:test];
这不太优雅,因为您必须使用字符串值指定变量,因此在拼写错误时不会收到编译器警告。
方法2:生成数组“objectArray”所需的KVO方法。
剪贴板上的访问器声明
在界面文件中的适当位置
剪贴板上的访问器定义
实现文件中的适当位置
然后您可以使用如下所示的方法
The problem is that your not correctly using KVC. There is two things you can do.
Method 1: Simple but not so elegant
[[self mutableArrayValueForKey:@"objectArray"] addObject:test];
This isn't so elegant as you have to specify the variable using a string value, so you will not get compiler warnings when spelt incorrectly.
Method 2: Generate the KVO methods needed for the array "objectArray".
accessor decls on Clipboard
appropriate spot in your interface file
accessor defs on Clipboard
appropriate spot in your implementation file
You can then use a method that looks like