NSCollectionView什么都没画

ind*_*gie 7 cocoa objective-c nsarraycontroller nscollectionview nscollectionviewitem

我正在尝试建立一个NSCollectionView(我过去成功完成了这个,但由于某种原因,这次失败了).

我有一个名为"TestModel"的模型类,它有一个NSString只返回一个字符串的属性(仅用于测试目的).然后NSMutableArray我在我的主app appate类中有一个属性声明,并且我在这个数组中添加了该TestModel对象的实例.

然后我有一个Array Controller,它的内容数组绑定了app delegate NSMutableArray.我可以确认到目前为止的一切工作正常; NSLogging:

[[[arrayController arrangedObjects] objectAtIndex:0] teststring]
Run Code Online (Sandbox Code Playgroud)

工作得很好.

然后,我为集合视图设置(itemPrototype和内容)以及集合视图项(视图)提供了所有适当的绑定.然后,我在集合项视图中有一个绑定到Collection View的文本字段Item.representedObject.teststring.但是,当我启动应用程序时,NOTHING会显示在集合视图中,只是一个空白的白色屏幕.我错过了什么?

更新:这是我使用的代码(由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
Run Code Online (Sandbox Code Playgroud)

然后像我说的那样,Array Controller的内容数组绑定到这个"objectArray",并且NSCollectionView的Content绑定到Array Controller.arrangedObjects.我可以通过NSLogging [arrayController arrangeObjects]验证Array Controller中是否有对象,并返回正确的对象.它只是在NSCollectionView中没有显示.

更新2:如果我记录[collectionView内容]我什么都没得到:

2009-10-21 08:02:42.385 CollViewTest[743:a0f] (
)
Run Code Online (Sandbox Code Playgroud)

可能存在问题.

更新3:这里要求的是Xcode项目:

http://www.mediafire.com/?mjgdzgjjfzw

它是一个菜单栏应用程序,所以它没有窗口.当您构建并运行应用程序时,您将看到一个名为"test"的菜单栏项,这将打开包含NSCollectionView的视图.

谢谢

Bra*_*oss 4

问题是你没有正确使用KVC。您可以做两件事。

方法一:简单但不太优雅

  1. 使用以下代码将对象添加到数组中

[[self mutableArrayValueForKey:@"objectArray"] addObject:test];

这不太优雅,因为您必须使用字符串值指定变量,因此在拼写错误时不会收到编译器警告。

方法2:生成数组“objectArray”所需的KVO方法。

  1. 在接口声明中选择属性
  2. 选择脚本(菜单栏中的脚本图标)> 代码 > 将访问器声明放在剪贴板上
  3. 将声明粘贴到接口文件中的适当位置
  4. 选择脚本 > 代码 > 将访问器定义放在剪贴板上
  5. 将定义粘贴到实施文件中的适当位置

然后您可以使用类似的方法

[self insertObject:test inObjectArrayAtIndex:0];
Run Code Online (Sandbox Code Playgroud)