NSMatrix绑定混乱

时间:2014-08-01 01:30:56

标签: cocoa-bindings nsmatrix

我希望使用NSMatrix来表示6天工作周(6行)中员工(列)的调度矩阵。仅供参考,因为NSTableView并不支持逐个单元格的拖放,我不得不求助于使用NSMatrix。长号。

无论如何,如果我想使用Cocoa绑定,那么NSArray content NSMatrix需要在{{1}}上横向工作。

如果我有一名员工,我的阵列将包含6个项目。得到它了。但是,如果我添加第二名员工,那么员工1的数据需要占用偶数数组索引(0-2-4-6-8-10),而员工2的数据占用奇数索引( 1-3-5-7-9-11)。

如果我现在要删除员工1,那么我需要按顺序删除项目10,8,6,4,2,0!

Yowza。

我读到这个吗?还有其他人不得不满足于这种疯狂吗?

1 个答案:

答案 0 :(得分:0)

实际上,我的初始断言是正确的 - 即,对于内容的基础数组,NSMatrix将内容从左到右,然后从上到下。将使用这些索引布置5x2矩阵中的10个元素的数组:

 0  1  2  3  4
 5  6  7  8  9

所以我与你分享了几个代码片段...例如,在这个结果中调用[self insColAtIndex:self.matrix.numberOfColumns],在新矩阵中添加了新项“X”:

 0  1  2  3  4  X
 5  6  7  8  9  X

如果没有进一步的麻烦,通过分配一个初始大小的NSMatrix至少有1行所需的n行高度,这里有两个方法在子类NSArrayController中进行控制{{ 1}}及其内容数组串联工作:

NSMatrix

我意识到我可以创建一个带有数组索引的// insert NSMatrix column at index (zero-based) -(void)insColAtIndex:(NSInteger)index { NSInteger colCount = self.matrix.numberOfColumns; if (index<0 || index>colCount) return; NSInteger rowCount = self.matrix.numberOfRows; [self.matrix addColumn]; for (NSInteger i=0; i<rowCount; i++) [self insertObject:[[NSCell alloc] init] atArrangedObjectIndex:index+colCount*i+i]; } // remove NSMatrix column at index (zero-based) -(void)delColAtIndex:(NSInteger)index { NSInteger colCount = self.matrix.numberOfColumns; if (index<0 || index>=colCount) return; NSInteger rowCount = self.matrix.numberOfRows; [self.matrix removeColumn:index]; for (NSInteger i=rowCount-1; i>=0; i--) [self removeObjectAtArrangedObjectIndex:index+colCount*i]; } 并一举影响NSMatrix的数组(使用NSMutableIndexSetinsertObjects:atArrangedObjectIndexes:),但这似乎比它更麻烦很值得。

从数组中插入/删除连续行的代码留给读者(更容易)练习; - )