Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a custom cell in a NSTableView

In my app I'm trying to create a custom cell like the follow:

enter image description here

I know how to do that with iOS and I guess it should be quite the same by using OS X. So in interface builder I designed the table view but I can figure how to create a custom cell. I tried to insert in the .xib in which I designed the table view the custom component I need (2 NSTextField and 2 NSImageView), then I create a class "CustomCell.m" and "CustomCell.h" as subclass of NSTableCellView, so I tried to connect my component to this class, but I can't add it... Why I can't connect the component to "CustomCell" class? What's wrong? Can you help me to find a way to do this (maybe a tutorial too)?

To do this I just followed the method I now to create custom table cell for iOS

like image 792
lucgian841 Avatar asked Jan 23 '15 15:01

lucgian841


3 Answers

  1. In the Xib, add a NSTableView and make sure the contentType is View Based in the Attributes Inspector pane.
  2. The Table column contains a TableCellview, which contains a TableViewCell by default. Remove the TableViewCell.
  3. Drag the NSTextFields and ImageViews into the TableCellview as needed. By default, NSTableCellview supports 1 Imageview and 1 Textfield. If you need two of each, inherit NSTableCellview and create IBOutlet properties for your components and change the class of NSTableCellview in IB to InheritedTableCellview.

    @interface InheritedTableCellview : NSTableCellView
    
    @property (assign) IBOutlet NSTextField *secondTextField;
    @property (assign) IBOutlet NSImageView *secondImageView;
    
    @end
    
    @implementation SRITableCellView
    
    @end
    
  4. Name the identifier of TableCellview with unique string.

  5. Connect the Imageview and Textfield outlet components to TableCellview.
  6. Connect the tableview Datasource and delegate to the viewController.

In the view controller, implement the below datasource method for display the number of rows required.

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
    return self.tableArray.count;
}

Implement the delegate method to set the image and Text for each row,

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    InheritedTableCellview *cellView = [tableView makeViewWithIdentifier:@"MainCell" owner:self];
    cellView.backgroundStyle = NSBackgroundStyleDark;
    cellView.textField.stringValue = self.tableArray[row][@"textValue1"];
    cellView.imageView.image = [NSImage imageNamed:self.tableArray[row][@"image1"]];
    cellView.secondTextField.stringValue = self.tableArray[row][@"textValue2"];
    cellView.secondImageView.image = [NSImage imageNamed:self.tableArray[row][@"image2"]];

    return cellView;
}
like image 62
Srinidhi Avatar answered Nov 05 '22 09:11

Srinidhi


In your xib, select your custom cell you want to connect, and go to the Identity Inspector in the Utilities area on the right panel (https://developer.apple.com/library/ios/recipes/xcode_help-general/Chapters/AbouttheUtilityArea.html). Change the class to be of type CustomCell. Then you should be able to connect it to an outlet

like image 33
A O Avatar answered Nov 05 '22 08:11

A O


Apple is discouraging the use of NSCell subclasses as of Lion. You can now do a NSView based NSTableView, which is much more flexible.

I've done them by creating NSViews in the dataSource methods but here's a decent tute on doing it with a nib.

see apple docs too.

like image 1
Jef Avatar answered Nov 05 '22 08:11

Jef