NSMatrix NSArrayController绑定问题---呃

时间:2014-08-17 18:39:07

标签: objective-c cocoa-bindings key-value-observing nsarraycontroller nsmatrix

我想操纵数组控制器的NSMutableArray属性,该属性作用于NSMatrix的内容,而不必手动将单元格添加到绑定到的NSMatrix

我花了很多时间在这上面,但无济于事。

任何帮助都非常赞赏。

我在NSMatrix模式下以编程方式创建了NSListModeMatrix。在我的NSArrayController子类中,我填充了NSMutableArray,其中包含4个虚拟对象,表示4行和1列数据。然后我将填充的NSMutableArray

绑定到该矩阵
interface:
@property (nonatomic, strong) NSMutableArray *myArray;

implementation (init):
NSMutableDictionary *bindingOptions = [NSMutableDictionary dictionary];
[bindingOptions setObject:[NSNumber numberWithBool:YES] forKey:NSInsertsNullPlaceholderBindingOption];
[bindingOptions setObject:[NSNumber numberWithBool:YES] forKey:NSRaisesForNotApplicableKeysBindingOption];

[matrix bind:@"content"
    toObject:self
 withKeyPath:@"myArray"
     options:bindingOptions];

现在我想在矩阵中添加列。对于第一次添加,这意味着在位置1,3,5,7处索引的一组单元格,其中col = 1。这是由于NSMatrix支持内容数组的从左到右,从上到下的性质:

NSInteger colCount = [matrix numberOfColumns];
NSInteger rowCount = [matrix numberOfRows];
NSMutableArray *newList = [[NSMutableArray alloc] init];
NSMutableIndexSet *myIndexes = [[NSMutableIndexSet alloc] init];
for (NSInteger i=0; i<rowCount; i++) {
    [newList addObject:[[NSCell alloc] init]];
    [myIndexes addIndex:col+colCount*i];
}

现在,我想做的就是:

  [self.myArray insertObjects:newList atIndexes:myIndexes];

希望NSMatrix会自动更新。但是,唉,没有。

我可以NSLog确认数组的大小正在增加(并且布局正确),但在UI中没有任何更新,除非我执行以下操作:

// update the `NSMatrix` manually
  [matrix insertColumn:col withCells:newList];
// update the underlying array
  [self willChange:NSKeyValueChangeInsertion valuesAtIndexes:myIndexes forKey:@"myArray"];
  [self.myArray insertObjects:newList atIndexes:myIndexes];
  [self didChange:NSKeyValueChangeInsertion valuesAtIndexes:myIndexes forKey:@"myArray"];

如果我遗漏第一行,NSMatrix完全是空白的。

如果我遗漏了第二个&amp;最后一行(willChange / didChange)行并保留第一行,矩阵仅显示默认的单列。

但是,在所有情况下,我都看到底层数组的大小和排列正确增长。

但我只想更新底层的可变数组,而不必自己将列添加到NSMatrix

如何让NSMatrix一起玩?

PS:我可以通过这样做将上面的4行缩短为2行,放弃意志/确实行:

  [matrix insertColumn:col withCells:newList];
  [[self mutableArrayValueForKey:@"myArray"] insertObjects:newList atIndexes:myIndexes];

0 个答案:

没有答案