【发布时间】:2017-01-28 16:38:00
【问题描述】:
我有一个需要按组呈现大量数据的应用。我在表格的角落视图中放置了一个组选择菜单,以允许用户选择要查看的组。
此表的列具有标识符 fld# 0..n,以及用于获取数据的关联控制器。在目标类中,它会 - 使用 IBOutlet 绑定到视图控制器检索组选择,并使用它通过 switch 选择要显示的值。
一切都很花哨,直到我需要支持多个视图/窗口实例。所以我想我会在运行时改变 ?在表列及其绑定中。到目前为止,我只通过 IB 做过这样的事情,所以这是我第一次涉足内部并陷入困境。
我的角落视图操作菜单(用户调用的内容):
- (NSMenu *)menuForEvent:(NSEvent *)event
{
NSMenu * menu = [[[NSMenu alloc] init] autorelease];
SEL action = @selector(cornerAction:);
NSMenuItem * item = nil;
int type = 0;
// We auto enable items as views present them
[menu setAutoenablesItems:YES];
// TableView level column customizations
for (NSString * title in titles)
{
BOOL state = dataView.data == type;
item = [[NSMenuItem alloc] initWithTitle:title
action:action
keyEquivalent:@""];
[item setRepresentedObject:dataView];
[item setState:state];
[item setEnabled:YES];
[item setTag:type++];
[item setTarget:dataView];
[item setAction:action];
[menu addItem:item];
[item release];
}
return menu;
}
然后采取行动 - 但失败了,因为我需要弄清楚如何更新绑定:
- (IBAction)cornerAction:(id)sender
{
// Configure which type of data to show, then columns' identifier and sort
self.data = (self == sender ? 0 : [sender tag]);
[super cornerAction:sender];
for (NSUInteger itm=0; itm<self.fieldCount; itm++)
{
NSString * fld = [NSString stringWithFormat:@"fld%@%d", titles[data], itm];
NSString * key = [NSString stringWithFormat:@"srt%@%d", titles[data], itm];
NSSortDescriptor * srt = [NSSortDescriptor sortDescriptorWithKey:key ascending:YES];
[cols[itm] setIdentifier:fld];
[cols[itm] setSortDescriptorPrototype:srt];
[cols[itm] bind:<#(nonnull NSString *)#>
toObject:<#(nonnull id)#>
withKeyPath:<#(nonnull NSString *)#>
options:<#(nullable NSDictionary<NSString *,id> *)#>]
}
[self reloadArray:YES];
}
cols[] 是一个表列数组,因此最后一行是将列与控制器(树控制器)的绑定更新为正确数据的起点。我更新了类以删除 fld# 占位符列并创建了 fld# 和 srt# ivars 的所有变体;这些基本上返回底层的 ivar。最后,所有访问都是只读的。
我在想我现在需要做的就是更新绑定。我也觉得可能不需要对标识符和排序描述符进行列更改?
无论如何,我试图避免使用选项卡的 B 计划,为每个组实例化 tableview - 呸,或者也许有更好的方法?
更新:假设我继续执行计划-A 不应该这样做:假设 cols[0] 的列标识符是 fld0
[cols[itm] bind: @"value"
toObject: treeController
withKeyPath: [NSString stringWithFormat:@"arrangedObjects.%@",fld]
options: nil]
【问题讨论】:
-
表格视图是基于视图还是基于单元格?
-
通常,如果您有一个绑定到控制器的表格,并且您想要更改表格中显示的内容,您可以更改控制器的内容......您不会拆除视图的绑定。而且,如果您希望以不同方式绑定和显示不同的数据集,您可能希望通过动态显示不同的视图来处理它,而不是使用重构绑定的同一视图。
-
所以,是的,这有点像将鸡蛋重新打乱成煎蛋卷,所以我认为最好使用几个选项卡的选项卡视图(无选项卡)。
-
这是一个基于单元格的视图。
标签: objective-c cocoa cocoa-bindings