objective-c - 我无法获取使用 XCode 5 的 Collection View 编程主题教程

标签 objective-c macos cocoa

我已经完成了三遍 Collection View 编程主题。一步一步,但我无法在 Collection View 中获取任何内容。

当我将 NSText 标签绑定(bind)到 returnedObject 时,我注意到了它们的绑定(bind)。是他们得到一个感叹号。

我尝试设置模型关键路径、xcode 标识符和标题,但无济于事。

除了 Collection View 项之外,我还获得了额外的 View 。

希望大家有想法。

我断言标签有字体,并且 gridview 有 0 行和 1 列。

这是AppDelegate.h

#import <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject <NSApplicationDelegate>
@property NSMutableArray *personModelArray;

@property (assign) IBOutlet NSWindow *window;
// To see if personModelArray is populated.
- (IBAction) showElements:(id) sender ;
@end

这是AppDelegate.m

//
//  AppDelegate.m
//  Occupations

#import "AppDelegate.h"
#import "PersonModel.h"
@implementation AppDelegate
@synthesize personModelArray;

- (void)awakeFromNib {

    PersonModel * pm1 = [[PersonModel alloc] init];
    pm1.name = @"John Appleseed";
    pm1.occupation = @"Doctor";

    PersonModel * pm2 = [[PersonModel alloc] init];
    pm2.name = @"Jane Carson";
    pm2.occupation = @"Teacher";

    PersonModel * pm3 = [[PersonModel alloc] init];
    pm3.name = @"Ben Alexander";
    pm3.occupation = @"Student";

    NSMutableArray * tempArray = [NSMutableArray arrayWithObjects:pm1, pm2, pm3, nil];
    [self setPersonModelArray:tempArray];
    /*
    for (PersonModel *obj in personModelArray) {
        NSLog(@"%@ %@", obj.name, obj.occupation) ;
    }
     */
    //  NSLog(@"Awoken from nib %@",personModelArray) ;

}
// Convenience: hooked up with a button to see that
// the contents of the array is still there!
-(void)showElements:(id) sender {
    for (PersonModel *obj in personModelArray) {
        NSLog(@"%@ %@", obj.name, obj.occupation) ;
    }

}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
}
-(void)insertObject:(PersonModel *)p inPersonModelArrayAtIndex:(NSUInteger)index {
    [personModelArray insertObject:p atIndex:index];
}

-(void)removeObjectFromPersonModelArrayAtIndex:(NSUInteger)index {
    [personModelArray removeObjectAtIndex:index];
}

-(void)setPersonModelArray:(NSMutableArray *)a {
    personModelArray = a;
}

-(NSArray*)personModelArray {
    return personModelArray;
}
@end

这是 PersonModel.h

//
//  PersonModel.h
//  Occupations
//

#import <Foundation/Foundation.h>

@interface PersonModel : NSObject {
    NSString * name;
    NSString * occupation;
}
@property  NSString * name;
@property  NSString * occupation;


@end

这是 PersonModel.m

//
//  PersonModel.m
//  Occupations


#import "PersonModel.h"

@implementation PersonModel
@synthesize name;
@synthesize occupation;
@end

最佳答案

我在 2013 年犯下的最大错误是我没有完全理解控件 (NSContentsView) 应该如何组装。

我刚刚偶然发现了 Cocoa Bindings 编程主题中的教程,但无法使其工作。为我辩护,首先我对绑定(bind)做了一些调整,所以我怀疑错误仍然存​​在于某个地方,但什么也没有。我已将绑定(bind)的控件放入内容 View 中,而不是“ GridView ”中。

关于objective-c - 我无法获取使用 XCode 5 的 Collection View 编程主题教程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20853926/

相关文章:

objective-c - 按日期排序 NSMutableArray,然后按字母顺序排序

macos - 展开和折叠 View 上的标准 Cocoa 控件

swift - 如何在 Mac OS Swift 应用程序中重新获得/设置焦点

objective-c - cocoa 自动化WebView

objective-c - 在整个 NSTableView/NSOutlineView 的字体大小后动态改变行高

iphone - Cocoa Touch 中的线条平滑

iphone - 向导航栏添加标题

ios - 我怎样才能使 UIView 透明与墙纸

macos - 如何在我的 visual studio 代码中使用 FSI for F# 4?

ios - 如何使用链接中的图像创建 UIImageView?