6

私はココアを初めて使用し、イライラしています。NSView を NSTableView セルに追加する方法を見つけるためにほぼ半日を費やしましたが、何をするのに役立つ良いガイドが見つかりませんでした私は達成したいと思います。おそらく、誰かが私が試したことを見て、なぜそれが機能しないのか、どうすれば機能させることができるのかを教えてくれます...

-(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 つの NSTextFields を互いの上に配置し、テーブル セルにカスタム背景を持たせることです。上記は、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];

NSTableViewsでWWDC 2011makeViewWithIdentifier:ビデオを見ましたが、まだよくわかりません。

さらに情報が必要な場合は、お問い合わせください

ありがとう

編集 最初の回答後:

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

}

しかし、それはまだ機能していませんか?

4

3 に答える 3

7

以下のコードは私の問題を解決しました:

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

}

助けてくれてありがとう:)

于 2012-11-11T20:08:21.997 に答える
1

セルを返すメソッドは、デキュー可能でない場合はセルを作成する必要があります (セルがないため)

// 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";
  }
于 2012-11-11T18:53:42.100 に答える