Len*_*ase 6 cocoa nsview nstableview
我是可可的新手,我很沮丧,我花了将近一半的时间试图找出如何将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;
}
Run Code Online (Sandbox Code Playgroud)
我想要实现的是在彼此之上有两个NSTextField,并且表格单元格具有自定义背景.以上是我只想让一个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];
Run Code Online (Sandbox Code Playgroud)
我有点困惑makeViewWithIdentifier:,我已经看过NSTableViews上的WWDC 2011视频,但我还是不太确定.
如果您需要更多信息,请询问
谢谢
编辑 后第一个回答:
-(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;
Run Code Online (Sandbox Code Playgroud)
}
但它仍然无法正常工作?
下面的代码解决了我的问题:
-(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;
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助:)
如果单元格不可出队(因为不存在),则返回单元格的方法必须使单元格成为可出队的
// 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";
}
Run Code Online (Sandbox Code Playgroud)