2012-11-11 9 views
5

私はココアを初めて使っていて、不満を募らせています。NSViewをNSTableViewセルに追加する方法を調べるのにほとんど半日を費やしましたが、私は私が達成したいものを行うことができます素敵なガイドでは、多分誰かが...私が試した何を見ていると、それは働いていない理由を教えてください、私はそれが仕事を得ることができるかできNSTableViewセル内のCocoa NSView

-(NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{ 
NSTableCellView *view = [tableView makeViewWithIdentifier:@"MyView" owner:self]; 
NSTextField *textfield = [[NSTextField alloc]initWithFrame:NSMakeRect(0, 0, 100, 30)]; 
[textfield setStringValue:predictate_search[row]]; 
[textfield setBackgroundColor:[NSColor redColor]]; 
[view addSubview:textfield]; 
[view setNeedsDisplay:YES]; 
return view; 
} 

私が達成したいのは、2つのNSTextFieldがお互いにあり、表のセルにカスタム背景があることです。上記の私はちょうど1 NSTextField作業を取得しようとしているが、運と...

NSTableViewは、プログラムによって作成されている:

NSScrollView *scrollView = [[NSScrollView alloc]initWithFrame:bg]; 
    [scrollView setHasVerticalScroller:YES]; 
    [self addSubview:scrollView]; 

    search_results = [[NSTableView alloc]initWithFrame:bg]; 
    NSTableColumn *column = [[NSTableColumn alloc] initWithIdentifier:@"id"]; 
    [[column headerCell] setStringValue:@"Cities"]; 
    [column setWidth:1000.0]; 

    [search_results addTableColumn:column]; 
    [search_results setDelegate:(id)self]; 
    [search_results setDataSource:(id)self]; 
    [search_results reloadData]; 

    [scrollView setDocumentView:search_results]; 

私はmakeViewWithIdentifier:のために入れて何を少し困惑しています、私はNSTableViews上のWWDC 2011ビデオを見ましたが、私はまだそれほど確かではありません。

あなたはより多くの情報が必要な場合は最初の答えの後

おかげ

EDIT を依頼してください:それはまだ動作していないが

-(NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{ 
NSTableCellView *view = [tableView makeViewWithIdentifier:[tableColumn identifier] owner:self]; 
if(view == nil){ 
    NSTableCellView *view = [[NSTableCellView alloc]initWithFrame:[tableView frame]]; 
    view.identifier = [tableColumn identifier]; 
} 
NSTextField *textfield = [[NSTextField alloc]initWithFrame:NSMakeRect(0, 0, 100, 30)]; 
[textfield setStringValue:predictate_search[row]]; 
[textfield setBackgroundColor:[NSColor redColor]]; 
[view addSubview:textfield]; 
[view setNeedsDisplay:YES]; 
return view; 

}

を?

答えて

1

あなたのそれはdequeableイマイチ場合(何も存在しないため)、細胞を返すメソッドは、セルを作る必要があります

// There is no existing cell to reuse so we will create a new one 
if (result == nil) { 

    // create the new NSTextField with a frame of the {0,0} with the width of the table 
    // note that the height of the frame is not really relevant, the row-height will modify the height 
    // the new text field is then returned as an autoreleased object 
    result = [[[NSTextField alloc] initWithFrame:...] autorelease]; 

    // the identifier of the NSTextField instance is set to MyView. This 
    // allows it to be re-used 
    result.identifier = @"MyView"; 
    } 
+0

私はそれを追加しましたが、まだ動作しません... –

+0

ただチェックしています...あなたは他の方法を実装しましたか? :: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Protocols/NSTableDataSource_Protocol/Reference/Reference.html –

+0

私は ' - (NSInteger)numberOfRowsInTableView:(NSTableView *)を実装しました。私が知っている限り、それは唯一の強制的なものです。 –

6

以下のコードは、私の問題を解決しました:助けるため

-(NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{ 
NSView *view = [tableView makeViewWithIdentifier:@"MyView" owner:self]; 

if (view == nil) { 
    view = [[NSView alloc]initWithFrame:NSMakeRect(0, 0, 100, 30)]; 
    NSTextField *result = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 25, 800, 25)]; 
    NSTextField *result2 = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 800, 25)]; 
    result.stringValue = [predictate_search objectAtIndex:row]; 
    result2.stringValue = @"UnitedKingdom"; 
    [view addSubview:result]; 
    [view addSubview:result2]; 
    [view setNeedsDisplay:YES]; 
} 

return view; 

} 

感謝を:)

0

これを見るだけで、誰かが将来この問題を抱えるような場合に備えて、私は答えを追加します。

ビューは、コードスニペットの2行目で定義されています。それが無ければ、それはifステートメントに落ち、ビューは再び定義されます。 inステートメントから出てくるビューは、ブラケットのスコープ内で定義され、プログラムがそのスコープを離れて元のnilを残すときに消滅するため、消えます。

これでもう少し問題が発生する可能性がありますが、それは際立っています。私は主題についていくつかの研究をしていて、この質問を見つけ、スコープの問題に気づいた。

関連する問題