Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 20, 2006, 03:50 PM   #1
Thomas Harte
macrumors 6502
 
Join Date: Nov 2005
Getting started with NSTableView

I am someone with past experience of C++ and MFC who is trying to get started with Objective-C and Cocoa development. I have some books ordered to help me, but they haven't turned up yet, so I'm stuck trying to figure stuff out from the XCode help and http://developer.apple.com/documentation/Cocoa/. I did the Currency Converter tutorial and now am sort of wandering the API around aimlessly.

Today I am trying to make a decent stab at a NSTableView. I have added an NSTableView to my .nib, and set my controller as a dataSource. I have added the two methods numberOfRowsInTableView and what I guess could be called objectValueForTableColumn to my controller and I am able to populate the NSTableView by returning sensible results from those two methods. I am returning NSStrings for objectValueForTableColumn.

I have two questions. Firstly, what is the "correct" way to use the value passed as an (NSTableColumn *) to objectValueForTableColumn? At the minute I'm just doing [[tableColumn headerCell] stringValue] and comparing that to what I know are the column headers. That works, but strikes me as a very roundabout way of doing things and prone to localisation problems.

Secondly, how do I stop my NSTableView from being editable? What I really want is to receive a message on a row double click that will be responded to by actions in my model class. I have not implemented setObjectValue, which I thought would be enough to prevent editing but it doesn't seem to be. I want dragging and dropping so I need to implement validateDrop and forDraggedRowsWithIndexes, but I haven't done that yet.

Thanks in advance, and sorry for asking what are probably very basic questions that will have easy and easily discoverable answers once I have books.
Thomas Harte is offline   0 Reply With Quote
Old Jan 20, 2006, 04:09 PM   #2
HexMonkey
Administrator
 
HexMonkey's Avatar
 
Join Date: Feb 2004
Location: New Zealand
Quote:
Originally Posted by Thomas Harte
Firstly, what is the "correct" way to use the value passed as an (NSTableColumn *) to objectValueForTableColumn? At the minute I'm just doing [[tableColumn headerCell] stringValue] and comparing that to what I know are the column headers. That works, but strikes me as a very roundabout way of doing things and prone to localisation problems.
You could use [tableColumn identifier] instead. The identifier is set in Interface Builder in the same place as the header title, and it needn't be localised.

Quote:
Originally Posted by Thomas Harte
Secondly, how do I stop my NSTableView from being editable? What I really want is to receive a message on a row double click that will be responded to by actions in my model class. I have not implemented setObjectValue, which I thought would be enough to prevent editing but it doesn't seem to be.
To prevent the cells from being editable, select each table column in Interface Builder and uncheck the "Editable" attribute in the inspector.

To handle a double click, add the following code in your awakeFromNib method:
Code:
[tableView setTarget:self];
[tableView setDoubleAction:@selector(doubleClickInTable:)];
The doubleClickInTable method (call it anything you want) would be implemented as follows:

Code:
- (void)doubleClickInTable:(id)sender
{
	int rowIndex = [sender selectedRow]; //Use selectedRowIndexes if you're supporting multiple selection
	
	//Handle the double click
}

Last edited by HexMonkey; Jan 21, 2006 at 02:25 PM.
HexMonkey is offline   0 Reply With Quote
Old Jan 20, 2006, 04:11 PM   #3
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
I would recommend looking at Bindings instead of a datasource but we'll skip that for now!

Set a tag for each header in Interface Builder and then use [tableColumn tag]==0 (or whatever the tag value is: they are ints). The tag won't change on localization. (and int comparisons are cheaper than NSString). Edit: beaten to it and with the correct answer too: columns don't seem to have tags

Per column (double click on the column until it highlights) there is an editable attribute in Interface Builder. Use that to stop it being editable

Any more questions?
robbieduncan is offline   0 Reply With Quote
Old Jan 21, 2006, 10:34 AM   #4
Thomas Harte
Thread Starter
macrumors 6502
 
Join Date: Nov 2005
Quote:
Originally Posted by HexMonkey
To handle a double click, add the following code in your awakeFromNib method: ...
I have implemented it exactly as you suggest, but I seem to be getting single clicks as well. Is there anything I can do about that?
Quote:
Originally Posted by robbieduncan
I would recommend looking at Bindings instead of a datasource but we'll skip that for now!
We'll have to as I haven't yet figured out anything whatsoever about Bindings! I'll make a note to check that topic out as soon as I have some nice tangible documentation though.
Quote:
Originally Posted by HexMonkey
You could use [tableColumn identifier] instead.
Quote:
Originally Posted by robbieduncan
Set a tag for each header in Interface Builder and then use [tableColumn tag]==0 (or whatever the tag value is: they are ints)
I've gone with naming my columns as numbers and the following:

int column = [[tableColumn identifier] UTF8String][0] - '0';

I'm only planning on three columns, so I don't have to worry about moving on from '9' to ';' or anything like that.

Last edited by Thomas Harte; Jan 21, 2006 at 10:35 AM. Reason: got my quote syntax wrong
Thomas Harte is offline   0 Reply With Quote
Old Jan 21, 2006, 10:38 AM   #5
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Quote:
Originally Posted by Thomas Harte
We'll have to as I haven't yet figured out anything whatsoever about Bindings! I'll make a note to check that topic out as soon as I have some nice tangible documentation though.
Start off with the Apple Documentation.

That will probably leave you dazed and confused so then take a look at a nice easy introductory tutorial.

Now you are probably beginning to get an understanding and some clear examples will help.
robbieduncan is offline   0 Reply With Quote
Old Jan 21, 2006, 02:32 PM   #6
HexMonkey
Administrator
 
HexMonkey's Avatar
 
Join Date: Feb 2004
Location: New Zealand
Quote:
Originally Posted by Thomas Harte
I have implemented it exactly as you suggest, but I seem to be getting single clicks as well. Is there anything I can do about that?
That's strange, the only way that happens for me is if the table view also has an action (as well as a double action). Try adding the code:
Code:
[tableView setAction:nil];
to the end of your awakeFromNib method and see if it fixes it. If it does, check your connections in Interface Builder and make sure you're not calling setAction elsewhere in your code.
HexMonkey is offline   0 Reply With Quote
Old Jan 21, 2006, 06:00 PM   #7
Thomas Harte
Thread Starter
macrumors 6502
 
Join Date: Nov 2005
Quote:
Originally Posted by robbieduncan
Start off with the Apple Documentation.
Although I am building on 10.4, I want to retain compatibility with 10.2 and 10.3 so I guess I can't use bindings in this project. It's actually really trivial on the code side and something I'm sure anyone with good Cocoa sense could knock up in half an afternoon. If it gets too much for me I'll just go back to the tutorials rather than asking stupid questions here...
Quote:
Originally Posted by HexMonkey
That's strange, the only way that happens for me is if the table view also has an action (as well as a double action). Try adding the code: ...
Interestingly that stopped the response to double clicks, so I guess I must have a connection set up in Interface Builder that I've just forgotten about. I can't find anything, but I'm still not 100% confident with that corner of the developer tools having just been using XCode to do the normal platform neutral stuff with SDL and so on until now.

Anyway, I just wanted to say thanks to both of you for the help. Thanks!
Thomas Harte is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
NSTableView get selected row data cpuin Mac Programming 2 Oct 2, 2013 11:08 AM
Edit NSTableView - NSArrayController kikko088 Mac Programming 0 Jan 17, 2013 08:57 AM
Call reloadData for NSTableView from AppDelegate AdrianK Mac Programming 3 Aug 11, 2012 01:32 PM
Reordering rows in a view-based NSTableVIew monsieurpaul Mac Programming 2 Jul 19, 2012 08:21 AM
Asynchronous NSURLConnections + NSTableView Help jwei92 Mac Programming 0 Jul 10, 2012 08:42 PM

Forum Jump

All times are GMT -5. The time now is 11:41 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC