Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c Cocoa-基于多视图的NSTableView_Objective C_Cocoa_Nstableview - Fatal编程技术网

Objective c Cocoa-基于多视图的NSTableView

Objective c Cocoa-基于多视图的NSTableView,objective-c,cocoa,nstableview,Objective C,Cocoa,Nstableview,我刚刚开始自学cocoa,我遇到了一个(可能很简单)问题:使用相同的委托和控制器(在我的例子中是应用程序委托)显示多个基于视图的NSTableView。我看到这个帖子: 但是所描述的方法仍然给我带来了错误——特别是 方法“numberOfRowsInTableView:”的重复声明 方法“tableView:viewForTableColumn:row:”的重复声明 显然,编译器没有看到不同的方法声明适用于不同的表视图 AppDelegate.m文件中TableView的代码为 @synthes

我刚刚开始自学cocoa,我遇到了一个(可能很简单)问题:使用相同的委托和控制器(在我的例子中是应用程序委托)显示多个基于视图的NSTableView。我看到这个帖子: 但是所描述的方法仍然给我带来了错误——特别是

方法“numberOfRowsInTableView:”的重复声明 方法“tableView:viewForTableColumn:row:”的重复声明

显然,编译器没有看到不同的方法声明适用于不同的表视图

AppDelegate.m文件中TableView的代码为

@synthesize tableView1;
@synthesize tableView2;

-(NSUInteger)numberOfRowsInTableView:(NSTableView *)tableView1
{
    return 1;
}

-(NSUInteger)numberOfRowsInTableView:(NSTableView *)tableView2
{
    return 2;
}

- (NSView *)tableView:(NSTableView *)tableView1 viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    NSTableCellView *resultForTable1 = [tableView1 makeViewWithIdentifier:tableColumn.identifier owner:self];
    resultForTable1.textField.stringValue = @"This should appear in the first tableView";
    return resultForTable1;
}

- (NSView *)tableView:(NSTableView *)tableView2 viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    NSTableCellView *resultForTable2 = [tableView2 makeViewWithIdentifier:tableColumn.identifier owner:self];
    resultForTable2.textField.stringValue = @"This should appear in the second tableView";
    return resultForTable2;
}
在我的AppDelegate.h文件中,我有:

@property (weak) IBOutlet NSTableView *tableView1;
@property (weak) IBOutlet NSTableView *tableView2;

这里我做错了什么?

您没有多次复制这些方法——您没有提供参数“tableView1”和“tableView2”,等等,表视图调用这些方法并将其自身作为参数发送。因此,如果要对多个表使用同一个委托,可以在委托方法中放入if语句,以确定哪个表发送了消息。为每个表声明一个IBOutlet,如果表1….,则执行(伪代码)。。。。否则,如果表2…等

我认为您误解了该答案中描述的方法

您得到一个编译器错误,因为您尝试两次实现相同的方法。以下都是相同方法的实现:

- (void)setBlah:(id)aBlah {
- (void)setBlah:(id)newBlah {
- (void)setBlah:(id)theNewBlah {
(id)
参数类型后面的参数指定的不同“名称”仅对该方法的实现块是局部的

您应该能够使用以下代码完成您想要做的事情:

@synthesize tableView1;
@synthesize tableView2;

- (NSUInteger)numberOfRowsInTableView:(NSTableView *)aTableView {
    if (aTableView == tableView1) return 1;
    else if (aTableView == tableView2) return 2;
    return 0;
}

- (NSView *)tableView:(NSTableView *)aTableView
     viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {

    NSTableCellView *tableCellView = [aTableView
                   makeViewWithIdentifier:tableColumn.identifier owner:self];
    if (aTableView == tableView1) {
        tableCellView.textField.stringValue = 
                 @"This should appear in the first tableView";
    } else if (aTableView == tableView2) {
        tableCellView.textField.stringValue = 
                 @"This should appear in the second tableView";
    }
    return tableCellView;
}

请注意,我确保将参数命名为
aTableView
,与实例变量不同,这样我就可以成功地将其与下面几行中的实例变量进行比较。

是否可以改用绑定?完美!!非常感谢,谢谢你的解释!!一旦我有了代表,我会投票支持这个!