· 7 years ago · Jan 13, 2019, 08:36 PM
1Cocoa : Custom TableView cell?
2- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{
3 NSScrollView * tableContainer = [[NSScrollView alloc] initWithFrame:NSMakeRect(self.window.frame.size.width-TABLEWIDTH, 0, TABLEWIDTH, self.window.frame.size.height)];
4 SpriteTable *sT = [[SpriteTable alloc]initWithFrame:NSMakeRect(self.window.frame.size.width-TABLEWIDTH, 0, TABLEWIDTH, self.window.frame.size.height)];
5 NSTableView *tableView = [[NSTableView alloc] initWithFrame: sT.bounds];
6
7 NSTableColumn* firstColumn = [[[NSTableColumn alloc] initWithIdentifier:@"firstColumn"] autorelease];
8 [[firstColumn headerCell] setStringValue:@"First Column"];
9 [tableView addTableColumn:firstColumn];
10
11 tableView.dataSource = self;
12 tableView.delegate = self;
13 [tableContainer setDocumentView:tableView];
14 tableContainer.autoresizingMask = NSViewHeightSizable | NSViewMinXMargin;
15 [self.window.contentView addSubview: tableContainer];
16
17
18}
19
20
21- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView{
22
23 return 4;
24}
25
26
27
28- (NSView *)tableView:(NSTableView *)tableView
29 viewForTableColumn:(NSTableColumn *)tableColumn
30 row:(NSInteger)row {
31
32 // get an existing cell with the MyView identifier if it exists
33 CustomCell *result = [tableView makeViewWithIdentifier:@"MyView" owner:self];
34
35
36 // There is no existing cell to reuse so we will create a new one
37 if (result == nil) {
38 NSLog(@"result = nil");
39
40 // create the new NSTextField with a frame of the {0,0} with the width of the table
41 // note that the height of the frame is not really relevant, the row-height will modify the height
42 // the new text field is then returned as an autoreleased object
43 //result = [[[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 250, 70)] autorelease];
44
45 // the identifier of the NSTextField instance is set to MyView. This
46 // allows it to be re-used
47 result.identifier = @"MyView";
48 }
49
50 // result is now guaranteed to be valid, either as a re-used cell
51 // or as a new cell, so set the stringValue of the cell to the
52 // nameArray value at row
53
54 result.imageView.image = [NSImage imageNamed:NSImageNameHomeTemplate];
55
56 // return the result.
57 return result;
58
59}
60
61@implementation suhasView
62@synthesize name,containerView;// container view contains ur subview
63- (NSView*) myView
64{
65 NSBundle *bundle = [NSBundle bundleForClass:[self class]];
66 NSNib *theNib = [[NSNib alloc] initWithNibNamed:@"suhas"bundle:bundle];
67 [theNib instantiateNibWithOwner:self topLevelObjects:nil];
68 return containerView;
69}
70
71- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
72{
73suhas *a=[[suhas alloc]initWithFrame:NSMakeRect(0,0, 40, 40)];
74NSView * v = [a myView];
75[a.name setStringValue:@"suhas"];
76return v;
77}...//sorry for my naming of the class:)