3

私は自分でココアを学び始めたばかりで、同じデリゲートとコントローラー (私の場合はアプリデリゲート) を持つ複数のビューベースの NSTableView を表示する (おそらく単純な) 問題に遭遇しています。この投稿を見ました: 複数の NSTableView(s) を処理するための最良 の方法ですが、説明されている方法ではまだエラーが発生します-具体的には

メソッド 'numberOfRowsInTableView:' の宣言が重複しています メソッド 'tableView:viewForTableColumn:row:' の宣言が重複しています

明らかに、コンパイラは、さまざまなメソッド宣言がさまざまなテーブル ビューに対応していることを認識していません。

AppDelegate.m ファイルのテーブルビューのコードは次のとおりです。

@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;

ここで何が間違っていますか?

4

2 に答える 2

4

I think you're misunderstanding the method described in that answer.

You're getting a compiler error because you are trying to implement the same method twice. The following would all be implementations of the same method:

- (void)setBlah:(id)aBlah {
- (void)setBlah:(id)newBlah {
- (void)setBlah:(id)theNewBlah {

The different "names" given to the parameter that follows the (id) parameter type are only local to the implementation block of that method.

You should be able to accomplish what you want to do using code like the following:

@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;
}

Notice that I made sure to name the parameter aTableView, something different from the instance variables, so that I can successfully compare it to the instance variables in the following lines.

于 2012-09-03T15:08:14.547 に答える
1

メソッドを複数回複製しないでください。「tableView1」、「tableView2」などの引数を指定していない場合、テーブル ビューはこれらのメソッドを呼び出し、自分自身を引数として送信します。複数のテーブルに同じデリゲートを使用する場合、どのテーブルがメッセージを送信したかを確認するために、デリゲート メソッドに if ステートメントを配置します。各テーブルに対して IBOutlet を宣言し、(疑似コードで) if table1 .... else if table2 ...etc を実行します。 .

于 2012-09-03T15:06:46.133 に答える