Nested NSNotification for NSTableViews - objective-c

I have two table views controlling the display/sorting of a collection of objects (namely, by Category and Localization, i.e. Lieu).
My problem is : I want the selection to be updated when the user clicks on a cell inside any of those Table Views (which is working just fine using NSTableViewDelegate), but I also want to restore the selection to the default one in the other Table View.
My problem is then obvious : every call to tableViewSelectionDidChange triggers another call to himself, which makes the result quiet unstable. Is there a way to prevent this call [tableViewCategory selectRowIndexes:[NSIndexSet indexSetWithIndex:0] byExtendingSelection:NO]; from triggering a notification ?
- (void)tableViewSelectionDidChange:(NSNotification *)aNotification
{
if ([[[aNotification object]identifier]isEqualToString:#"table2"]){
//First, reset AnnonceWithCategory
[tableViewCategory selectRowIndexes:[NSIndexSet indexSetWithIndex:0] byExtendingSelection:NO];
//Then
[self showAnnoncesWithLieu];
}
else if ([[[aNotification object]identifier]isEqualToString:#"table3"]){
//First, reset AnnonceWithLieu
[tableViewLieu selectRowIndexes:[NSIndexSet indexSetWithIndex:0] byExtendingSelection:NO];
[self showAnnoncesWithCategory];
}
}

You can't prevent the NSTableView from sending the notification, but you can prevent your class from responding to it. You could do something like this:
- (void)tableViewSelectionDidChange:(NSNotification *)aNotification
{
if ([[[aNotification object]identifier]isEqualToString:#"table2"] && ! _currentlyUpdatingTable2){
//First, reset AnnonceWithCategory
_currentlyUpdatingTable2 = YES;
[tableViewCategory selectRowIndexes:[NSIndexSet indexSetWithIndex:0] byExtendingSelection:NO];
_currentlyUpdatingTable2 = NO;
//Then
[self showAnnoncesWithLieu];
}
else if ([[[aNotification object]identifier]isEqualToString:#"table3"] && ! _currentlyUpdatingTable3){
//First, reset AnnonceWithLieu
_currentlyUpdatingTable3 = YES;
[tableViewLieu selectRowIndexes:[NSIndexSet indexSetWithIndex:0] byExtendingSelection:NO];
_currentlyUpdatingTable3 = NO;
[self showAnnoncesWithCategory];
}
}
...where _currentlyUpdatingTable2 and _currentlyUpdatingTable3 are ivars of the object receiving the notification.

Related

draggingEntered not firing

I have a subclass of NBox that I would like to drag around a canvas called dragBox. I don't understand why draggingEntered isn't being fired on the following code. I get a nice slideback image, but none of the destination delegates are getting fired. Why?
-(void) awakeFromNib
{
[[self superview] registerForDraggedTypes:[NSArray arrayWithObject:NSFilenamesPboardType]];
}
-(void) mouseDown:(NSEvent *)theEvent
{
[self dragImage:[[NSImage alloc] initWithContentsOfFile:#"/Users/bruce/Desktop/Untitled-1.png"] at:NSMakePoint(32, 32) offset:NSMakeSize(0,0) event:theEvent pasteboard:[NSPasteboard pasteboardWithName:NSDragPboard] source:self slideBack:YES];
}
-(NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender // validate
{
NSLog(#"Updated");
return [sender draggingSourceOperationMask];
}
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender {
NSLog(#"Drag Entered");
return [sender draggingSourceOperationMask];
}
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender {
NSLog(#"Move Box");
[self setFrameOrigin:[sender draggingLocation]];
return YES;
}
-(BOOL) prepareForDragOperation:(id<NSDraggingInfo>)sender
{NSLog(#"Prepared");
return YES;
}
In your mouseDown method you are not putting anything in the pasteboard before you initiate the drag operation. The documentation for NSView states you need to add your data on the pasteboard before sending it to that message.
Whatever destination views you have are, or should be, registering for a certain drag type. If your pasteboard does not have any matching data for that type, the destinations will not fire any of the NSDraggingProtocol messages.
Solved!
I was using the NSBox as both a destination and source. The events weren't being fired when this was the case. I moved registerDragTypes to the superview, the canvas, and implemented the draggingEntered and performDrag there. It works now...
Bruce

Make NSTableView active

Cocoa noob here.
I've got a simple Mac app that includes an NSTextField that fetches some results and puts them into an NSTableView. I'd like to be able to press up/down while in the text field to activate the first/last item in the table view.
I've done the following:
- (void)keyUp:(NSEvent *)theEvent {
switch([theEvent keyCode]) {
case 125: {
NSLog(#"I need to move down");
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:0];
[resultsTableView selectRowIndexes:indexSet byExtendingSelection:NO];
[resultsTableView becomeFirstResponder];
break;
}
case 126: {
NSLog(#"I need to move up");
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:[results count]-1];
[resultsTableView selectRowIndexes:indexSet byExtendingSelection:NO];
[resultsTableView becomeFirstResponder];
break;
}
}
}
Which is partially what I want. It does select the first or last item in the resultsTableView, but the selected item stays grayed out, and the textview stays active.
I thought calling becomeFirstResponder on the resultsTableView would do the trick, but it didn't.
Any help would be greatly appreciated!
Never call becomeFirstResponder directly. Use NSWindow's makeFirstResponder:
[[resultsTableView window] makeFirstResponder:resultsTableView];

Clickable Text with Hover Effect in NSTableView

I'm trying to create a column of clickable URL-type text (NOT URLs like this, but essentially a borderless, title button or text field cell with a tracking area for a hover effect) within an NSTableView.
1.) When the user hovers over a particular cell the text in that cell should draw an underline below the text (hover/trackable area effect).
2.) When the user clicks the text it should perform an action.
I've subclassed NSCell and NSTableView and added a tracking area within the custom tableview to try and track the mouse location of the individual cell of the table to notify the cell when to redraw itself. I can get the current row and column of the mouse location, but can't seem to get the right cell in my custom tableview's mouseMoved: method
-(void)mouseMoved:(NSEvent *)theEvent {
[super mouseMoved:theEvent];
NSPoint p = [self convertPoint:[theEvent locationInWindow] fromView:nil];
long column = [self columnAtPoint:p];
long row = [self rowAtPoint:p];
id cell = [[self.tableColumns objectAtIndex:column] dataCellForRow:row];
}
It gets the cell for the column, but doesn't get the right cell for that particular row. Perhaps I'm not fully understanding the dataCellForRow: function for NSTableColumn?
I know you can't quite add a tracking area for cells, but instead you must create the hit test for mouse clicks and then begin tracking once the hit test is successful (meaning the mouse is already down) and then use startTracking:, continueTracking:, and stopTracking: to get the mouse's position. The idea though is that it has a hover effect before any mouseDown: action.
Also, I can't just use a view-based tableview (which would be incredible) because my app must be 10.6 compatible.
I'm not sure what's wrong with your method of getting the cell, but you don't really need to get that to do what you want. I tested a way to do this that entailed creating a table view subclass to do the tracking in the mouse moved method. Here is the code for that subclass:
-(void)awakeFromNib {
NSTrackingArea *tracker = [[NSTrackingArea alloc] initWithRect:self.bounds options:NSTrackingMouseEnteredAndExited|NSTrackingMouseMoved|NSTrackingActiveInActiveApp owner:self userInfo:nil];
[self addTrackingArea:tracker];
self.rowNum = -1;
}
-(void)mouseMoved:(NSEvent *)theEvent {
NSPoint p = theEvent.locationInWindow;
NSPoint tablePoint = [self convertPoint:p fromView:nil];
NSInteger newRowNum = [self rowAtPoint:tablePoint];
NSInteger newColNum = [self columnAtPoint:tablePoint];
if (newColNum != self.colNum || newRowNum != self.rowNum) {
self.rowNum = newRowNum;
self.colNum = newColNum;
[self reloadData];
}
}
-(void)mouseEntered:(NSEvent *)theEvent {
[self reloadData];
}
-(void)mouseExited:(NSEvent *)theEvent {
self.rowNum = -1;
[self reloadData];
}
I put the array and table delegate and data source code in the app delegate (probably not the best place, but ok for testing).
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
self.theData = #[#{#"name":#"Tom",#"age":#"47"},#{#"name":#"Dick",#"age":#"21"},#{#"name":#"Harry",#"age":#"27"}];
[self.table reloadData];
self.dict = [NSDictionary dictionaryWithObjectsAndKeys:#2,NSUnderlineStyleAttributeName,[NSColor redColor],NSForegroundColorAttributeName,nil];
}
- (NSInteger)numberOfRowsInTableView:(RDTableView *)aTableView {
return self.theData.count;
}
- (id)tableView:(RDTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex {
if (self.table.colNum == 0 && rowIndex == self.table.rowNum && [aTableColumn.identifier isEqualToString:#"Link"]) {
NSString *theName = [[self.theData objectAtIndex:rowIndex] valueForKey:#"name"];
return [[NSAttributedString alloc] initWithString:theName attributes:self.dict];
}else if ([aTableColumn.identifier isEqualToString:#"Link"]){
return [[self.theData objectAtIndex:rowIndex] valueForKey:#"name"];
}else{
return [[self.theData objectAtIndex:rowIndex] valueForKey:#"age"];
}
}
- (void)tableViewSelectionDidChange:(NSNotification *)aNotification {
if (self.table.colNum == 0)
NSLog(#"%ld",[aNotification.object selectedRow]);
}
I use the delegate method tableViewSelectionDidChange: to implement the action if you click on a cell in the first column (which has the identifier "Link" set in IB).

Is Empty selection in an NSTableView with Source List Highlighting not allowed?

I feel like I may be missing something obvious here, but if I have an NSTableView with it's Highlight set to Source List and with Empty selection enabled, I don't seem to be able to click on a blank row in the table to clear the selection.
Changing the Highlight to regular fixes the problem, but of course doesn't draw in the manner I'd like.
The table has no bindings and uses a custom data source. Is there a way to work around this limitation?
For now, I've ended up adding the following to my NSTableView subclass:
- (void)mouseDown:(NSEvent *)theEvent
{
[super mouseDown:theEvent];
if ( [self allowsEmptySelection] && [self selectionHighlightStyle] == NSTableViewSelectionHighlightStyleSourceList )
{
NSInteger row = [self rowAtPoint:[self convertPoint:[theEvent locationInWindow] fromView:nil]];
if ( row == -1 )
{
[self deselectAll:nil];
}
}
}
You can use target actions to accomplish this. During initialization do the following:
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.target = self;
self.tableView.action = #selector(singleClickAction:);
self.tableView.allowsEmptySelection = YES;
Then add a method to your class:
- (void)singleClickAction:(id)sender
{
NSInteger clickedRow = [sender clickedRow];
if (clickedRow < 0) {
[self.tableView deselectAll:self];
}
}

Creating No Empty Selections in NSCollectionView

I have set up an NSCollectionView in a cocoa application. I have subclassed the collection view's NSCollectionViewItem to send me a custom NSNotification when one of its views it selected / deselected. I register to receive a notification within my controller object when this notification is posted. Within this method I tell the view that has just been selected that it is selected and tell it to redraw, which makes it shade itself grey.
The NSCollectionViewItem Subclass:
-(void)setSelected:(BOOL)flag {
[super setSelected:flag];
[[NSNotificationCenter defaultCenter] postNotificationName:#"ASCollectionViewItemSetSelected"
object:nil
userInfo:[NSDictionary dictionaryWithObjectsAndKeys:(ASListView *)self.view, #"view",
[NSNumber numberWithBool:flag], #"flag", nil]];}
The Controller Class (in the -(void)awakeFromNib Method):
//Register for selection changed notification
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(selectionChanged:)
name:#"ASCollectionViewItemSetSelected"
object:nil];
And the -(void)selectionChanged:(NSNotification *)notification method:
- (void)selectionChanged:(NSNotification *)notification {
// * * Must get the selected item and set its properties accordingly
//Get the flag
NSNumber *flagNumber = [notification.userInfo objectForKey:#"flag"];
BOOL flag = flagNumber.boolValue;
//Get the view
ASListView *listView = [notification.userInfo objectForKey:#"view"];
//Set the view's selected property
[listView setIsSelected:flag];
[listView setNeedsDisplay:YES];
//Log for testing
NSLog(#"SelectionChanged to: %d on view: %#", flag, listView);}
The application that contains this code requires there to be no empty selection within the collection view at any time. This is where i get my problem. I've tried checking when a view's selection is changed and reselecting it if there is no selection, and manually selecting the views using NSCollectionView's
-(void)setSelectionIndexes:(NSIndexSet *)indexes
But there is always a situation which occurs that causes there to be an empty selection in the collection view.
So I was wondering if there is an easier way to prevent an empty selection occurring in an NSCollectionView? I see no checkbox in interface builder.
Thanks in advance!
Ben
Update
I ended up just subclassing my NSCollectionView, and overriding the - (void)mouseDown:(NSEvent *)theEvent method. I only then sent the method [super mouseDown:theEvent]; if the click was in one of the subviews. Code:
- (void)mouseDown:(NSEvent *)theEvent {
NSPoint clickPoint = [self convertPoint:theEvent.locationInWindow fromView:nil];
int i = 0;
for (NSView *view in self.subviews) {
if (NSPointInRect(clickPoint, view.frame)) {
//Click is in rect
i = 1;
}
}
//The click wasnt in any of the rects
if (i != 0) {
[super mouseDown:theEvent];
}}
I ended up just subclassing my NSCollectionView, and overriding the - (void)mouseDown:(NSEvent *)theEvent method. I only then sent the method [super mouseDown:theEvent]; if the click was in one of the subviews. Code:
- (void)mouseDown:(NSEvent *)theEvent {
NSPoint clickPoint = [self convertPoint:theEvent.locationInWindow fromView:nil];
int i = 0;
for (NSView *view in self.subviews) {
if (NSPointInRect(clickPoint, view.frame)) {
//Click is in rect
i = 1;
}
}
//The click wasnt in any of the rects
if (i != 0) {
[super mouseDown:theEvent];
}}
I also wanted to avoid empty selection in my collection view.
The way I did it is also by subclassing, but I overrode -hitTest: instead of -mouseDown: to return nil in case the click wasn't on an item :
-(NSView *)hitTest:(NSPoint)aPoint {
// convert aPoint in self coordinate system
NSPoint localPoint = [self convertPoint:aPoint fromView:[self superview]];
// get the item count
NSUInteger itemCount = [[self content] count];
for(NSUInteger itemIndex = 0; itemIndex < itemCount; itemIndex += 1) {
// test the point in each item frame
NSRect itemFrame = [self frameForItemAtIndex:itemIndex];
if(NSPointInRect(localPoint, itemFrame)) {
return [[self itemAtIndex:itemIndex] view];
}
}
// not on an item
return nil;
}
Although I'm late to this thread, I thought I'd just chime in because I've had the same problem recently. I got around it using the following line of code:
[_collectionView setValue:#NO forKey:#"avoidsEmptySelection"];
There is one caveat: the avoidsEmptySelection property is not part of the official API although I think that it's pretty safe to assume that its the type of property that will stick around for a while.