NSTableView Drawing Count - objective-c

I have two tables (NSTableView) on a window. Well, the number is irrelevant. Anyway, the source of the first table (tableView1) is NSMutableArray with NSMutableDictionary. And I have the following code.
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView {
if (aTableView == tableView1) {
return [itemArray1 count];
} else {
...
}
}
- tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)TableColumn row:(NSInteger)rowIndex {
// tableView1
if (aTableView == tableView1) {
NSString *foldername = [[itemArray1 objectAtIndex:rowIndex] objectForKey:key1a];
NSString *count = [[itemArray1 objectAtIndex:rowIndex] objectForKey:key1c];
if ([[TableColumn identifier] isEqualToString:#"folder"]) {
return foldername;
}
if([[TableColumn identifier] isEqualTo:#"count"]){
return count;
}
else {
return #"";
}
// tableView2
} else {
...
}
}
I get what I want, and the second column shows the number of items (count). I wonder how I can possibly draw this number like the SidebarDemo example project? (Shown at the bottom...) This project utilizes - (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item to show a static number (42). Can I show the number like that with NSTableView as well? I could just draw a circled number if it were an iOS application.
Thank you for your advice.

tableView:viewForTableColumn:row: (NSTableViewDelegate) should work similar.

Related

NSOutlineView Data Source not called in Mac OS 10.10

My app has a cell-based NSOutlineView which its data source is a NSMutableArray. In the initialisation, my app loads the NSMutableArray from a text file. Then, the NSOutlineView will be populated with the NSMutableArray.
NSOutlineView is populated with the NSMutableArray prior to OSX 10.10. In OSX 10.10, the NSOutlineView is empty, i.e. no data is displayed. I put the NSLog in the data source methods and found that in 10.10, only - (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item is called. The other 3 data source methods are not called.
I don't have OSX 10.10 to debug for this issue. Please help.
#interface AppController : NSObject {
...
IBOutlet NSOutlineView *outlineViewCmdSet;
}
#pragma OutlineView for CmdSet
// Data Source methods
- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
return (item == nil) ? [cmdTree count] : [item numberOfChildren];
}
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
return (item == nil) ? YES : [item isExpandable];
}
- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item {
return (item == nil) ? [cmdTree objectAtIndex:index] : [[item children] objectAtIndex:index];
}
- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {
return [item nodeName];
}
// Delegate methods
- (BOOL)outlineView:(NSOutlineView *)outlineView shouldEditTableColumn:(NSTableColumn *)tableColumn item:(id)item {
return NO;
}
// Drag command to ScriptEditor
- (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pboard {
// Single Selection. items array has only 1 object
TreeItem *item = [items objectAtIndex:0];
if ([item isExpandable]) // Group Name
{
return NO;
}
else // Command Name
{
// Copy the Command Name to the pasteboard.
[pboard declareTypes:[NSArray arrayWithObject:CommandTreeViewDataType]
owner:self];
[pboard setString:[item nodeName] forType:CommandTreeViewDataType];
return YES;
}
}
Thanks everyone. I have fixed this issue by moving the initialisation of the Mutable Array from applicationDidFinishLaunching to awakeFromNib.
Strangely enough, prior to 10.10, applicationDidFinishLaunching is called before the OutlineView data source methods but not in 10.10. Anybody has any idea of this?

EXC_BAD_ACCESS with NSOutlineView and stringWithFormat

I am experiencing a bad access error when an item of my NSOutlineView is expanded. When NSStrings are allocated with stringWithFormat:, there is an EXC_BAD_ACCESS error when expanding the outline. When they are replaced with strings in the form of #"string", there is no error.
I assume something is releasing with ARC, but I don't know how to keep it from happening. What doesn't look right here?
-(NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
if(!item)
return [_characterList count];
else if( [item isKindOfClass:[Character class]] )
return 3;
return 0;
}
-(BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
if( [item isKindOfClass:[Character class]] )
return YES;
return NO;
}
-(id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item {
if (!item)
return (Character*)[_characterList objectAtIndex:index];
else {
NSLog(#"%#", item);
Character *characterItem = (Character*)item;
switch (index) {
case 0:
return [NSString stringWithFormat:#"Api key: %#", [characterItem apiKey]];
break;
case 1:
return [NSString stringWithFormat:#"Access Mask: %#", [characterItem mask]];
break;
case 2:
return #"Last Updated: today";
break;
default:
break;
}
}
return nil;
}
-(id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {
if([item isKindOfClass:[Character class]])
return [(Character*)item name];
else
return item;
return nil;
}
The solution I have come up with (but don't particularly like). Replace outlineView:objectValueForTableColumn:byItem: with the following code.
-(NSView*)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item {
NSTableCellView *cell = [outlineView makeViewWithIdentifier:#"characterColumn" owner:self];
if([item isKindOfClass:[Character class]]) {
[cell.textField setStringValue:[item name]];
} else if([item isKindOfClass:[NSString class]]) {
[cell.textField setStringValue:item];
}
return cell;
}
Basically what this does is exactly what I'd expect the cell code to do, but it appears to retain things properly. Any insight from the masses?
EDIT: Here's the deal. NSOutlineView's dataSource delegate methods are a little bit more particular about ownership. It's not something that you have to deal with usually with a vanilla NSTableView, since there are not multiple levels of item. Basically, you need to create all of the objects for display elsewhere and make sure they are managed in memory elsewhere, because NSOutlineViewDataSource isn't going to do any of that for you.

Two NSTableCellView with different sizes appearing with the same size

Here are my 2 NSTableCellViews:
The 1st is bigger than the 2nd (this one is used when the user is searching the tableview and there are no results) but when i run my app the 2nd gets the size of the 1st:
Why is this happening? Here's my part of the code
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
if (noResults){
return 1;
}else{
return [array count];
}
}
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
if (noResults){
NSTableCellView *result = [tableView makeViewWithIdentifier:#"NoResults" owner:self];
result.textField.stringValue = [NSString stringWithFormat:#"No results for \"%#\"", _searchTXT.stringValue];
return result;
}else{
SearchTableCell *result = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];
result.textField.stringValue = #"Name";
result.textField2.stringValue = #"Last Name";
return result;
}
}
You need to implement the tableView:heightForRowAtIndexPath: method. Otherwise the cell will all be the height of the table views rowHeight property.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (noResults){
// return NSTableCellView height here;
}else{
// return SearchTableCell height here;
}
}
Hope this helps you.
For a view based NSOutlineView, implement the delegate method outlineView:heightOfRowByItem: to return the desired row height.
To avoid constants within your code, consider determining the height from the associated view:
- (CGFloat)outlineView:(NSOutlineView *)outlineView heightOfRowByItem:(id)item
{
NSView* view = [self outlineView:outlineView viewForTableColumn:nil item:item];
return (view ? NSHeight(view.frame) : outlineView.rowHeight);
}

How do i make a simple view-based NSOutlineView?

For learning purposes i would like to convert a cell-based NSOutlineView to a view-based one,
basically i would like the following:
instead of a normal cell, i'd like an 'image and text table cell view'
the image can be the stock NSApplicationIcon and the text can just be 'hello world' :)
I'd like to do this without using bindings and NSTreeController
Here is the 'worlds simplest NSOutlineView' example http://www.cocoasteam.com/Cocoa_Steam/Worlds_Simplest_Demo.html
I wonder if someone could modify it to make it view-based and work like i said above :) :)
I've tried looking at apple examples, and searching elsewhere on the internet but i still can't get it to work - so thanks very much in advance :)
I have created a little sample project which does just that.
Display a list of items
Edit the items in a master-detail fashion
Remove and add items
Usage of bindings
Check out besi/mac-quickies on github.
Most of the stuff is either done in IB or can be found in the AppDelegate
OK, so you want an NSOutlineView with ImageAndTextCell cells, right?
Let's do one of the most typical examples of this kind : a simple file explorer.
What we'll need :
an NSOutlineView (put an outline to your AppDelegate, as fileOutlineView)
create 3 columns in the Outline with the following Identifiers (set them up in Interface Builder) : NameColumn, SizeColumn, ModifiedColumn
Now, as for the rest, I'll do it all programmatically, so that you get a good idea of what's going on...
How to set it up (e.g. in - (void)awakeFromNib):
// set the Data Source and Delegate
[fileOutlineView setDataSource:(id<NSOutlineViewDataSource>)self];
[fileOutlineView setDelegate:(id<NSOutlineViewDelegate>)self];
// set the first column's cells as `ImageAndTextCell`s
ImageAndTextCell* iatc = [[ImageAndTextCell alloc] init];
[iatc setEditable:NO];
[[[fileOutlineView tableColumns] objectAtIndex:0] setDataCell:iatc];
Connecting the dots :
/*******************************************************
*
* OUTLINE-VIEW DATASOURCE
*
*******************************************************/
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
{
if ([item isFolder])
return YES;
else
return NO;
}
- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
{
if (item==nil)
{
// Root
return [[filePath folderContentsWithPathAndBackIgnoringHidden] count];
}
else
{
if ([item isFolder])
{
return [[item folderContentsWithPathAndBackIgnoringHidden] count];
}
else
{
return 0;
}
}
}
- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
{
if (item == nil)
{
// Root
return [[filePath folderContentsWithPathAndBackIgnoringHidden] objectAtIndex:index];
}
if ([item isFolder])
{
return [[item folderContentsWithPathAndBackIgnoringHidden] objectAtIndex:index];
}
// File
return nil;
}
- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)theColumn byItem:(id)item
{
if ([[theColumn identifier] isEqualToString:#"NameColumn"])
{
return [item lastPathComponent];
}
else if ([[theColumn identifier] isEqualToString:#"SizeColumn"])
{
if ([item isFolder]) return #"--";
else return [NSString stringWithFormat:#"%d",[item getFileSize]];
}
else if ([[theColumn identifier] isEqualToString:#"ModifiedColumn"])
{
if ([item isFolder]) return #"";
else return [NSString stringWithFormat:#"%#",[item getDateModified]];
}
// Never reaches here
return nil;
}
/*******************************************************
*
* OUTLINE-VIEW DELEGATE
*
*******************************************************/
- (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item
{
return YES;
}
- (BOOL)outlineView:(NSOutlineView *)outlineView isGroupItem:(id)item
{
return NO;
}
- (void)outlineView:(NSOutlineView *)outlineView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item {
[cell setDrawsBackground:NO];
if ([item isFileHidden]) [cell setTextColor:[NSColor grayColor]];
else [cell setTextColor:[NSColor whiteColor]];
if ([[tableColumn identifier] isEqualToString:#"NameColumn"])
{
if ([item isFolder])
[cell setImage:[[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kGenericFolderIcon)] size:15.0];
else
[cell setImage:[[NSWorkspace sharedWorkspace] iconForFile:item] size:15.0];
if ([item isFileHidden])
{
[cell setFileHidden:YES];
}
else
{
[cell setFileHidden:NO];
}
}
}
Hint : ImageAndTextCell class can be found here. You'll also notice a few other methods I'm using, which are obviously NOT supported by Cocoa (e.g. isFileHidden, isFolder or folderContentsWithPathAndBackIgnoringHidden) but it's not that difficult to implement them yourself...)
To return view to OutlineView column Instead of using datasource method that return objectValue:
- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)theColumn byItem:(id)item
USE THE DATASOURCE METHOD THAT RETURN VIEW!!!!!!!!:
- (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item
everything else is the same(minimal req is the first three datasource methods, you don't need the delegate methods) but,
you can't use willdisplaycell its called only for cell based , do everything to the view in the viefortablecolumn method like this:
if ([[tableColumn identifier] isEqualToString:#"YourColumnIdentifier"]){
NSTableCellView *cell = [outlineView makeViewWithIdentifier:#"YourViewsIdentifier" owner:self];
[cell.textField setStringValue:[(YourItem *)item name]];
[cell.imageView setImage:[(YourItem *)item image]];
return cell;
}
return nil;
and don't forget to set identifiers , and to set the OutlineView to be View Based(in IB ...).
Check TableViewPlayground, also View Based NSTableView Basic to Advanced from WWDC 2011.

Data for NSOutlineView Source List

i've got a Delegate class for a Source List. But i don't know what type the return variable of outlineView:objectValueForTableColumn:byItem: should be.
At the Moment my code looks like this, all the structure things work but there is no text shown:
#interface DataSource : NSObject<NSOutlineViewDelegate,NSOutlineViewDataSource>
#end
And the .m
#implementation DataSource
// Data Source methods
- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
return (item == nil) ? 1 : [item numberOfChildren];
}
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
return (item == nil) ? YES : ([item numberOfChildren] != -1);
}
- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item {
return (item == nil) ? [FileSystemItem rootItem] : [(FileSystemItem *)item childAtIndex:index];
}
//-(id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
-(id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {
return #"Some String";
}
#end
I have made a example app to show the difference. Image is here
I suppose you have view-based NSTableView. In you delegate you should implement method - (id)outlineView:(NSOutlineView *)ov viewForTableColumn:(NSTableColumn *)tableColumn. It may looks like this:
- (id)outlineView:(NSOutlineView *)ov viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item{
if ([[item representedObject] parent] == nil) {
return [ov makeViewWithIdentifier:#"HeaderCell" owner:self];
}else{
return [ov makeViewWithIdentifier:#"DataCell" owner:self];
}
}
HeaderCell and DataCell are default identifiers of the Table Cell Views.