【问题标题】:Cocoa : Custom TableView cell?可可:自定义 TableView 单元格?
【发布时间】:2012-10-07 17:33:33
【问题描述】:

我对表格不是很熟悉,因为我通常制作游戏,但现在我想创建一个关卡构建器,我需要一个带有自定义单元格的表格视图。我创建了一个 nib 文件,并且将 NSTableCellView 子类化,但我不知道下一步该做什么。我只有:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{
    NSScrollView * tableContainer = [[NSScrollView alloc] initWithFrame:NSMakeRect(self.window.frame.size.width-TABLEWIDTH, 0, TABLEWIDTH, self.window.frame.size.height)];
    SpriteTable *sT = [[SpriteTable alloc]initWithFrame:NSMakeRect(self.window.frame.size.width-TABLEWIDTH, 0, TABLEWIDTH, self.window.frame.size.height)];
    NSTableView *tableView = [[NSTableView alloc] initWithFrame: sT.bounds];

    NSTableColumn*  firstColumn = [[[NSTableColumn alloc] initWithIdentifier:@"firstColumn"] autorelease];
    [[firstColumn headerCell] setStringValue:@"First Column"];
    [tableView  addTableColumn:firstColumn];

    tableView.dataSource = self;
    tableView.delegate = self;
    [tableContainer setDocumentView:tableView];
    tableContainer.autoresizingMask = NSViewHeightSizable | NSViewMinXMargin;
    [self.window.contentView addSubview: tableContainer];


}


- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView{

    return 4;
}



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

    // get an existing cell with the MyView identifier if it exists
    CustomCell *result = [tableView makeViewWithIdentifier:@"MyView" owner:self];


    // There is no existing cell to reuse so we will create a new one
    if (result == nil) {
        NSLog(@"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:NSMakeRect(0, 0, 250, 70)] autorelease];

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

    // result is now guaranteed to be valid, either as a re-used cell
    // or as a new cell, so set the stringValue of the cell to the
    // nameArray value at row

    result.imageView.image = [NSImage imageNamed:NSImageNameHomeTemplate];

    // return the result.
    return result;

}

如果有的话,我必须实现哪些委托方法?以及如何使用 nib 文件自定义单元格?

【问题讨论】:

    标签: macos cocoa nstableview nstablecellview


    【解决方案1】:

    在你的子视图中执行此操作->

    @implementation suhasView 
    @synthesize name,containerView;// container view contains ur subview
    - (NSView*) myView 
    {
      NSBundle *bundle = [NSBundle bundleForClass:[self class]];
      NSNib *theNib = [[NSNib alloc] initWithNibNamed:@"suhas"bundle:bundle];
     [theNib instantiateNibWithOwner:self topLevelObjects:nil];
     return containerView;
    }
    

    在控制器中->

    - (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn   *)tableColumn row:(NSInteger)row 
    { 
    suhas *a=[[suhas alloc]initWithFrame:NSMakeRect(0,0, 40, 40)];
    NSView * v = [a myView]; 
    [a.name setStringValue:@"suhas"];
    return v;
    }...//sorry for my naming of the class:)  
    

    【讨论】:

    • 非常感谢,但我刚刚在我的窗口中创建了一个表格并且它成功了,所以我想我不需要这个。但是,如果可行,我会尝试并接受您的回答:)
    • 这样做:)..如果你接受书面回答,它将鼓励我们在未来帮助你:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多