Get selected row and column in view based nstable - macos

I have a view based NSTable with ten or more rows and five columns. To populate data for this table, I have a dictionary of 5 arrays. So each dictionary object is for each column.
Now I need to get information when I click on any cell. I am using tableViewSelectionDidChange delegate to get notified on cell selection. Using this I can get only the [myTableView selectedRow] to get to know the row selected. How can I know which column was the row in. For ex: I select/click on 5th row in 3rd column. Now I need to know which column was it clicked on so that I can extract required object from the dictionary.
Any clues ?

You may override the mouseDown: event of your tableView and get the clicked column as:
- (void)mouseDown:(NSEvent *)theEvent
{
NSPoint globalLocation = [theEvent locationInWindow];
NSPoint localLocation = [self convertPoint:globalLocation fromView:nil];
NSInteger clickedCol = [self columnAtPoint:localLocation];
[super mouseDown:theEvent];
}
or you may use NSTableView delegate method:
tableView:didClickTableColumn:
For reference check:
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/NSTableViewDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/NSTableViewDelegate/tableView:didClickTableColumn:

Related

Getting a column in an NSTableView with editable column order

I have an NSTableView where I would like to be notified if the user clicks in a column "ClickMe". I linked the entire table view to a method which can extract the clickedColumn:, but I get an absolute number and not a reference to the "ClickMe" column (which may have been moved to another place).
I could of course program my own search algorithm to see if column X is actually the "Clickme" column, but that would not be very elegant. Is there a way to identify columns properly, and to receive that ID programmatically?
I found a way to do my own search in a fairly fast way, but I still have a feeling I am putting too much effort in this:
First, set the Identifier of the desired column in the Interface Builder to "ClickMeColumn". Then:
NSInteger cmColumn = [tableView columnWithIdentifier:#"ClickMeColumn"];
if ( [tableView clickedColumn] == cmColumn )
NSLog(#"Clicked me!");
I am looking for something along the lines of [tableView clickedColumnIdentifier].
What about querying NSTableView's columnAtPoint: in your table views mouseDown: or mouseUp: method?
Use any of the methods below. Called by the tableView's delegate on selection. You can extract the identifier and the title string from the relevant tableColumn.
- (void)tableView:(NSTableView *)tableView didClickTableColumn:(NSTableColumn *)tableColumn {
NSLog(#"tableView:didClickTableColumn: %#, titleString: %#", [tableColumn identifier], [[tableColumn headerCell] stringValue]);
}
- (void)tableView:(NSTableView *)tableView mouseDownInHeaderOfTableColumn:(NSTableColumn *)tableColumn {
NSLog(#"tableView:mouseDownInHeaderOfTableColumn: %#, titleString: %#", [tableColumn identifier], [[tableColumn headerCell] stringValue]);
}
From: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSTableView_Class

How to add row-span in view-based NSTableView?

I'm trying to get a table layout as shown below working with view-based NSTableViews that have been introduced in Lion.
There were multiple approaches described for cell-based NSTableViews, e.g. Mimic the artwork column, but these don't really apply for view based table views.
The idea is that the table gets populated by an array of objects, in one (or more) column spanning rows indicating that objects share some data in common. numberOfRowsInTableView: returns to total number of items (19 in the case of the attached image).
Has anyone tried something like this?
Layout
I was able to do this by using two separate NSTableViews that have their NSScrollView's scrolling synchronized. To learn how to synchronize multiple scroll view's (with the exact subclass code) read Scroll View Programming Guide for Mac - Synchronizing Scroll Views
I have groupTableView that has a single column and shows the views that represent the group. I also have itemTableView that has columns representing the items of the group. I use the same delegate/datasource methods with if-else-statements to check which NSTableView is being used and respond accordingly with the proper # of rows, cell view, etc.. Additionally, I implement the following delegate method to adjust the group table view's row height to be equal the sum of the item row heights of the rows in the group:
- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row
{
if (tableView == self.groupTableView) {
NSUInteger firstRowIndexForGroup = ...;
NSUInteger lastRowIndexForGroup = ...;
CGFloat groupHeight = 0.0;
for (NSUInteger currentRowIndex = firstRowIndexForGroup; currentRowIndex <= lastRowIndexForGroup; currentRowIndex++) {
groupHeight += [self.itemTableView rectOfRow:lastRowIndexForGroup].size.height;
}
return groupHeight - [self.itemTableView intercellSpacing].height;
} else {
return self.itemTableView.rowHeight;
}
}
You must also call -noteHeightOfRowsWithIndexesChanged: every time the table view needs a group view because the height changes according to the number of rows in the group.
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
if (tableView == self.groupTableView) {
GroupRowView *view = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];
// configure view
[tableView noteHeightOfRowsWithIndexesChanged:[NSIndexSet indexSetWithIndex:row]];
return view;
} else {
ItemRowView *view = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];
// configure view
return view;
}
}
I disabled row selection and horizontal and vertical scroll bars on the group table view. I also made the horizontal grid solid for both table views. Finally, I position the group table view directly next to the item table view with no gap in between so it appears as if it's simply another column in a single table view. The result is a flawless implementation that has zero lag between the table views. To the user, it appears as if it's a single table view.
Could you not use an NSOutlineView instead? It's specifically designed to have grouping support.

NSOutlineView: How to Put a Specific Cell Into Edit Mode? (editColumn Method Not Working as Expected)

I have an NSOutlineView. When I edit a cell in it and hit return, I would like the cell directly below it to go immediately into edit mode.
I thought I would use the controlTextDidEndEditing: delegate method to find out when editing of a cell had finished, and the editColumn:row:withEvent:select: method to put the cell below it into edit mode.
Here's a demo method that I pasted into "appcontroller.m" of the Apple demo code project, "DragNDropOutlineView":
- (void)controlTextDidEndEditing:(NSNotification *)aNotification
{
NSTreeNode* treeNodeJustEdited;
NSInteger indexOfCurrentRow = [outlineView selectedRow];
if (indexOfCurrentRow < 0) //no row selected
return;
treeNodeJustEdited = [outlineView itemAtRow:indexOfCurrentRow];
NSTreeNode* theRootNode = rootTreeNode;
NSInteger numberOfChildren = [[theRootNode mutableChildNodes]count];
NSInteger indexOfLastChild = numberOfChildren - 1;
if (indexOfCurrentRow < indexOfLastChild)
{
[outlineView selectRowIndexes:[NSIndexSet indexSetWithIndex:indexOfCurrentRow+1] byExtendingSelection:NO];
[outlineView editColumn:0 row: indexOfCurrentRow+1 withEvent:nil select:NO];
}
}
For some reason, this does the following:
Correctly highlights the row below the cell
just edited. But then:
Disables that
highlighting, i.e. puts it in light
grey
Makes the cell below the cell
just edited, invisible. Clicking
twice on that cell returns the table
to normal.
What am I missing in order to get this method to perform as desired?
Here is another question, it is similar with yours
Hope helpfully.
NSTableView & NSOutlineView editing on tab key

detail item from didSelectRowAtIndexPath comes out as null

Right now I have a table with a number of PDFs listed. From the table the user can select a PDF and it will be displayed in the view. Right now when I select and item from the table it should change the detailView's detailItem to the item in the row I selected, in addition change the variable i to the selected row number.
Here is what my DidSelectRowAtIndexPath method looks like:
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
affirmaPDFViewController.detailItem = [NSString stringWithFormat:#"%#", [listOfPDF objectAtIndex:indexPath.row]];
affirmaPDFViewController.i = indexPath.row;
NSLog(#"%d", indexPath.row); //returns the proper value
NSLog(#"%#", [listOfPDF objectAtIndex:indexPath.row]); //returns the proper value
//NSLog(#"%d", affirmaPDFViewController.i); //DOES NOT return the proper value
//NSLog(#"%#", affirmaPDFViewController.detailItem); //DOES NOT return the proper value
}
for the first two NSLog's, they display the proper information, however when i check the last two they come out with affirmaPDFViewController.i = 0 and affirmaPDFViewController.detailItem = null. When i need the affirmaPDFViewController.i= indexPath.row, and affirmaPDFViewController.detailItem = the item in the row I selected.
Anyone know why they aren't coming out with the proper values?
Thanks in advance!
Your instance of affirmaPDFViewController is nil. Check the method where it's created, and remember you can set a breakpoint in the debugger to see if it's a valid object or not.

NSTableView setting the sort column?

I have a NSTableView with multiple columns. clicking each of the columns sorts by the column like in iTunes. However when the tableview first loads the rows are unsorted and no tablecolumn is highlighted or displaying the up/down indicator image. I'm wondering if theres a simple way I can programmatically set the column the table is sorted by and set the indicator image on startup.
The only solution I can think of is using [NSTableView setIndicatorImage: inTableColumn:] and [NSTableView setHighlightedColumn:], but that makes it so that clicking on the header doesnt highlight the column. I would rather not have to use tableView:mouseDownInHeaderOfTableColumn: and rewrite the whole click on header to sort thing.
You might try setting your sort discriptor.
- (void)setSortDescriptors:(NSArray *)array
- (void)windowControllerDidLoadNib:(NSWindowController *) windowController
{
[super windowControllerDidLoadNib:windowController];
NSSortDescriptor* sortDescriptor = [[[NSSortDescriptor alloc] initWithKey: #"order" ascending: YES] autorelease];
[oTable setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
}
http://lists.apple.com/archives/cocoa-dev/2006/May/msg01434.html

Resources