嵌套NSCollectionView绑定的 [英] Nested NSCollectionView With Bindings

查看:205
本文介绍了嵌套NSCollectionView绑定的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图巢NSCollection查看彼此的内部。我曾尝试使用来创建一个新项目的<一个href=\"http://developer.apple.com/library/mac/#DOCUMENTATION/Cocoa/Conceptual/CollectionViews/Introduction/Introduction.html\"相对=nofollow称号=快速入门指南>苹果快速入门指南的位置。

我通过插入一个集合视图到我的笔尖,以自动添加我将另一个集合视图上的视图开始。添加的子集观点得到了一些标签。这里是我的笔尖的图片:

我再回去,并建立我的模型:
我的第二级车型的.h是

  @interface BPG_PersonModel:NSObject的@财产(保留,读写)的NSString *名称;
@财产(保留,读写)的NSString *职业;@结束

我的第一级车型的.h是:

  @interface BPG_MultiPersonModel:NSObject的@财产(保留,读写)的NSString *组名;
@财产(保留,读写)的NSMutableArray * personModelArray; - (NSMutableArray的*)setupMultiPersonArray;@结束

然后我写出来的实施,使第一级控制器内的一些打假人(建立第二级模型):
(编辑)删除awakefromnib code

  / *  - (无效){awakeFromNib    BPG_PersonModel * PM1 = [[BPG_PersonModel的alloc]初始化];
    pm1.name = @约翰苹果核战记;
    pm1.occupation = @医生;    //类似code这里PM2,PM3    NSMutableArray里* tempArray = [NSMutableArray的arrayWithObjects:PM1,PM2,PM3,零]
    [个体经营setPersonModelArray:tempArray];} * /
- (NSMutableArray的*)setupMultiPersonArray {
    BPG_PersonModel * PM1 = [[BPG_PersonModel的alloc]初始化];
    pm1.name = @约翰苹果核战记;
    pm1.occupation = @医生;    //类似code这里PM2,PM3
    NSMutableArray里* tempArray = [NSMutableArray的arrayWithObjects:PM1,PM2,PM3,零]
    返回tempArray;
}

最后我做一个类似的实现在我的appdelegate打造的多人阵列

   - (无效){awakeFromNib    self.multiPersonArray = [[NSMutableArray的页头] initWithCapacity:1];    BPG_MultiPersonModel * mpm1 = [[BPG_MultiPersonModel的alloc]初始化];
    mpm1.groupName = @一日清单;
    mpm1.personModelArray = [mpm1 setupMultiPersonArray]

(我不是包括所有code在这里,让我知道这将是有益的。)

然后我所推荐的快速入门指南绑定的一切。我添加两个nsarraycontrollers带有属性添加到阵列控制器的每个级别控制器对象绑定

然后我使用绑定到arrangedobjects内容绑定的CollectionView到阵列控制器

最后,我绑定子视图:

与grouptitle标签在我的模型重新presentedobject.grouptitle对象

然后我的名字和职业标签,以各自的再presentedobjects

我把所有的对象志愿合规性,包括必要的存取方法

然后我尝试运行这个程序,并第一个错误我得到的是: NSCollectionView项目原型不能是零

(编辑),从第一级模型我得到这个去掉awakefromnib后

任何人都已经成功地嵌套nscollection看法?我在做什么错在这里?下面是压缩为别人测试完整的项目:

http://db.tt/WPMFuKsk

感谢您的帮助。

编辑:

我终于联系上了苹果的技术支持,看看他们是否能帮助我。
从他们的反应是:


  

Cocoa绑定将只能走这么远,直到你需要一些额外的code,使这一切的工作。


  
  

在使用数组内的数组来填充您的收藏查看
  绑定将无法正确传送到每个复制视图
  没有继承NSCollectionView并覆盖
  newItemForRe presentedObject和实例相同的厦门国际银行自己,
  而不是使用提供的视图复制实现
  NSCollectionView。


  
  

所以在使用newItemForRe presentedObject方法,您需要
  我们因素的NSCollectionViewItems为独立的xibs这样你
  可以从组集合视图传递下来的人子阵
  你内心的集合视图。


  
  

因此​​,对于您的收藏分组查看您的覆盖看起来是这样的:

   - (NSCollectionViewItem *)newItemForRe presentedObject:(ID)对象
{
BPG_MultiPersonModel *模型=对象;
MyItemViewController *项目= [[MyItemViewController页头] initWithNibName:@GroupPrototype捆绑:无];
item.re presentedObject =对象;
item.personModelArray = [[NSArrayController的页头] initWithContent:model.personModelArray];
归还物品;
}

和你的内心集合的子类覆盖时是这样的:

   - (NSCollectionViewItem *)newItemForRe presentedObject:(ID)对象
{
PersonViewController *项目= [[PersonViewController页头] initWithNibName:@PersonPrototype捆绑:无];
item.re presentedObject =对象;
归还物品;
}


下面是他们发回给我一个示例项目 -

http://db.tt/WPMFuKsk

我仍然无法得到这与我自己的项目工作。可以在项目中,他们发回进一步简化?


解决方案

请仔细看看这个回答

简短的回答:
每提取到的NSView它自己的.xib应该解决这个问题。

扩展:
该IBOutlet中的你NSCollectionViewItem子类时指定原型复制未连接。那么,我们如何连接IBOutlet中的我们NSCollectionViewItem子类的控件视图中指定?

Interface Builder中把自定义的NSView在同一笔尖的NSCollectionView和NSCollectionViewItem。这是愚蠢的。解决的办法是对的NSView移动到它自己的笔尖,并获得所述控制器加载编程的观点:


  • 将在一个NSView到它自己的笔尖(从而打破了NSCollectionViewItem和一个NSView之间的连接)。

  • 在I.B.,文件所有者的身份类更改为NSCollectionViewItem子类。

  • 控件连接到文件所有者网点。

  • 最后得到NSCollectionViewItem子类加载笔尖:

有用的链接:


  1. 如何从头开始编程方式创建nscollectionview
  2. nscollectionview提示

  3. 尝试嵌套一个nscollectionview失败

  4. nscollectionview终极版

I am trying to nest NSCollection view inside of one another. I have tried to create a new project using the Apple Quick Start Guide as a base.

I start by inserting a collection view into my nib, to the view that is automatically added I drag another collection view onto it. The sub-collection view added gets some labels. Here is a picture of my nib:

I then go back and build my models: My second level model .h is

@interface BPG_PersonModel : NSObject

@property(retain, readwrite) NSString * name;
@property(retain, readwrite) NSString * occupation;

@end

My First level model .h is:

@interface BPG_MultiPersonModel : NSObject

@property(retain, readwrite) NSString * groupName;
@property(retain,readwrite) NSMutableArray *personModelArray;

-(NSMutableArray*)setupMultiPersonArray;

@end

I then write out the implementation to make some fake people within the first level controller(building up the second level model): (edit) remove the awakefromnibcode

/*- (void)awakeFromNib {

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

    //similar code here for pm2,pm3

    NSMutableArray * tempArray = [NSMutableArray arrayWithObjects:pm1, pm2, pm3, nil];
    [self setPersonModelArray:tempArray];

} */


-(NSMutableArray*)setupMultiPersonArray{
    BPG_PersonModel * pm1 = [[BPG_PersonModel alloc] init];
    pm1.name = @"John Appleseed";
    pm1.occupation = @"Doctor";

    //similar code here for pm2,pm3


    NSMutableArray * tempArray = [NSMutableArray arrayWithObjects:pm1, pm2, pm3, nil];
    return tempArray;
}

Finally I do a similar implementation in my appdelegate to build the multiperson array

- (void)awakeFromNib {

    self.multiPersonArray = [[NSMutableArray alloc] initWithCapacity:1];

    BPG_MultiPersonModel * mpm1 = [[BPG_MultiPersonModel alloc] init];
    mpm1.groupName = @"1st list";
    mpm1.personModelArray = [mpm1 setupMultiPersonArray];

(I'm not including all the code here, let me know if it would be useful.)

I then bind everything as recommended by the quick start guide. I add two nsarraycontrollers with attributes added to bind each level of array controller to the controller object

I then bind collectionview to the array controller using content bound to arrangedobjects

Finally I bind the subviews:

with the grouptitle label to representedobject.grouptitle object in my model

then my name and occupation labels to their respective representedobjects

I made all the objects kvo compliant by including the necessary accessor methods

I then try to run this app and the first error I get is: NSCollectionView item prototype must not be nil.

(edit) after removing awakefromnib from the first level model I get this

Has anyone been successful at nesting nscollection views? What am I doing wrong here? Here is the complete project zipped up for others to test:

http://db.tt/WPMFuKsk

thanks for the help

EDITED:

I finally contacted apple technical support to see if they could help me out. Response from them is:

Cocoa bindings will only go so far, until you need some extra code to make it all work.

When using arrays within arrays to populate your collection view the bindings will not be transferred correctly to each replicated view without subclassing NSCollectionView and overriding newItemForRepresentedObject and instantiating the same xib yourself, instead of using the view replication implementation provided by NSCollectionView.

So in using the newItemForRepresentedObject approach, you need to factor our your NSCollectionViewItems into separate xibs so that you can pass down the subarray of people from the group collection view to your inner collection view.

So for your grouped collection view your override looks like this:

- (NSCollectionViewItem *)newItemForRepresentedObject:(id)object 
{ 
BPG_MultiPersonModel *model = object; 
MyItemViewController *item = [[MyItemViewController alloc] initWithNibName:@"GroupPrototype" bundle:nil]; 
item.representedObject = object; 
item.personModelArray = [[NSArrayController alloc] initWithContent:model.personModelArray]; 
return item; 
} 

And for your inner collection subclass your override looks like this:

- (NSCollectionViewItem *)newItemForRepresentedObject:(id)object 
{ 
PersonViewController *item = [[PersonViewController alloc] initWithNibName:@"PersonPrototype" bundle:nil]; 
item.representedObject = object; 
return item; 
}

here is a sample project that they sent back to me -

http://db.tt/WPMFuKsk

I am still unable to get this to work with my own project. Can the project they sent back be simplified further?

解决方案

Please take a closer look at this answer

Short answer: Extracting each NSView into its own .xib should solves this issue.

Extended: The IBOutlet’s specified in your NSCollectionViewItem subclass are not connected when the prototype is copied. So how do we connect the IBOutlet’s specified in our NSCollectionViewItem subclass to the controls in the view?

Interface Builder puts the custom NSView in the same nib as the NSCollectionView and NSCollectionViewItem. This is dumb. The solution is to move the NSView to its own nib and get the controller to load the view programmatically:

  • Move the NSView into its own nib (thus breaking the connection between the NSCollectionViewItem and NSView).
  • In I.B., change the Class Identity of File Owner to the NSCollectionViewItem subclass.
  • Connect the controls to the File Owner outlets.
  • Finally get the NSCollectionViewItem subclass to load the nib:

Usefull links:

  1. how to create nscollectionview programatically from scratch
  2. nscollectionview tips
  3. attempt to nest an nscollectionview fails
  4. nscollectionview redux

这篇关于嵌套NSCollectionView绑定的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆