Handling custom selection style in view based NSTableView - objective-c

How do I go about drawing my own custom selection style for a view based NSTableView? I tried putting a BOOL var in my NSTableCellView subclass and set that to YES if it is clicked and then I can successfully draw my custom selection. But how do I change that BOOL var to NO when another view is clicked? Thanks for any help.
EDIT: After reading through the NSTableView docs, it looks like I need to subclass NSTableRowView to override the selection drawing, but what do I do with my NSTableRowView subclass? How do I get the table to use it?

Alright, I figured it out. You just have to subclass NSTableRowView. It has methods for drawing the background for selected and deselected rows. To get the table view to use your subclass just implement the table view delegate method tableView:rowViewForRow: and return an instance of your subclass.

To make things clear, I think we should give the code of the delegate method :
- (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row
{
MyNSTableRowView *rowView = [[MyNSTableRowView alloc]init];
return rowView;
}

Related

Do I need to subclass the Image&Text NSTableCellView to add a label?

I have a view-based NSTableView (using the Image&Text CellView).
Depending on the dataItems in the dataSource, I select an appropriate CellView to display them.
One of the templates has an additional (to the default CellView) label on it. But I don't know what is the right way to access it when I instantiate the CellView (I need to set label's value).
My question basically is - should I search for the subview (like in the following snippet) or should I subclass the CellView, add a property and so on?
NSArray* subviews = [cellView subviews];
for (id view in subviews) {
ViewType* view = (ViewType*)view;
if ( view != nil ) {
if ( [view.identifier isEqualToString:#"idSetInIB"] ) {
[view setStringValue:dataItem.someValue];
}
}
}
Thanks.
The Apple TableView Programming guide gives good basic examples on how to subclass NSTableCellView for a view based table.
It's probably a little easier to do without bindings.
And certainly better for learning it.
However, you should be able to just add another text field in Interface Builder to the table cell view.
Read the guide. It will help tremendously.

Changing UILabel text from UICollectionView custom delegate class

In my storyboard I have a UICollectionView, with cells, sections, etc. I set as delegate and datasource a custom class (so, not the classic myCollectionView.delegate = self) but a custom class that cares about the retrieving of datas.
In this UICollectionView I have a label that I need to update with a certain value that I handle in the delegate class. (It's a property of the selected cell).
How can this be done? I read about KVO and Notification, but i'm not sure what I have to do.
More Detail
MatchCollectionViewController
In this one:
viewDidLoad
A gameView that act as a container for the following collectionView
CollectionView declaration (once created the collectionView i add it to the gameView)
The delegate of the CollectionView is the following class CollectionViewUiGrid
CollectionViewUiGrid
In this class i implement all the delegate and dataSource methods for the collectionView
initWithFrame (and initWithFrame with my own parameters)
in the method cellForItemAtIndexPath i return an istance of a class (UiGameCell) that extends UiCollectionViewCell
In the method didSelectItemAtIndexPath i have this logic.
If a cell is selected, assign retrieve his property, called CellDescription).
This is the property that i would like to be as my UiLabel value in MatchCollectionViewController.
I found a solution.
As mentioned in
Pass object with NSNotificationCenter to other view
Probably one of the simplest way is to use a NsNotificationCenter.

NSStepper in NSTableCellView has no effect

I’m using a regular (not subclassed) NSTableCellView in a view-based table view. It has the initial image and text field views. I added an NSStepper to the view.
The text field is bound to tableCellView.objectValue.quantity.
The stepper’s value is bound to tableCellView.objectValue.quantity too.
The problem is that when running the app, when I click the stepper it doesn’t seem to get the mouse event, neither arrow gets highlighted, the value is not incremented or decremented.
If I set the double action of the table view it gets triggered if I double-click the stepper as if it was transparent.
What am I missing?
Thanks!
You should look at the documentation but easiest is that you need to subclass NSTableView and override this method to validate the proposed first responder. As the document states NSTableViews disallow some controls to be used unless the row is first selected. Even then it still may discard some.
- (BOOL)validateProposedFirstResponder:(NSResponder *)responder forEvent:(NSEvent *)event {
return YES;
}
Further to the correct answer from Robert Payne, with Swift you could add an extension to NSTableView and not subclass it.
extension NSTableView {
override public func validateProposedFirstResponder(responder: NSResponder, forEvent event: NSEvent?) -> Bool {
return true
}
}
And I'd like to emphasis that it's the NSTableView not the NSTableViewCell.

Custom NSButton and NSButtonCell

I'm trying to create a custom button look.
From what I've gathered, NSButtonCell does the drawing, so I should actually be overwriting that instead.
But the issue is, my CustomButton class has other things like NSImage, mIsMouseOver etc. Currently the drawing is being done in the CustomButton class but I want to move it over to the cell.
question is, is there anyway I can access the image in the customButton class from the customButtonCell class so that I may use [image drawInRect:...]?
Regards,
Han
Your cell's drawWithFrame:(NSRect)frame inView:(NSView *)controlView method includes a reference to the NSView being drawn from which you can access the view's properties (such as image).
Usual way is to store the data in the NSCell subclass. Base cell class even has an -(id)image property, so, your button class should call [[self cell] image] when it is queried for image.
Actually, since you are subclassing NSButton, it contains all you need, just override cell's drawing methods. And if you need an extra property - define it in the cell, wrap in the control.

Loading Accessory callout view for mkannotationview

I have a map annotation view that contains a rightcallout button which loads an accessory view which is a UIViewController class. I am using resuable annotations, but am wondering how I can pass updated information to my UIViewController class. Let's say I have 2 string values which map to 2 UILabels on my view. How can I update those values after the initial accessory view has already been loaded into memory as a resusable view?
Any help would be appreciated.
You'll need to maintain a reference to the UILabels in the object that gets the update, and then use setTitle: (I think) to update the labels.
In your annotation subclass you need to override the setTitle method to send the changes to the instance of your UIViewController class that your subclass is holding. Or, you could setup your annotation subclass to receive notifications (from NSNotificationCenter), and upon receiving a notification, update the title and the instance of your UIViewController class.
If you are unfamiliar with NSNotifications, then here is a quick reference. I used these to keep my annotations updated.
NSNotification Example
Try using the MKMapViewDelegate method:
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control;
This method is called when a user tapped one of the annotation view’s accessory buttons. Assuming that your MKMapViewDelegate is also the UIViewController that can access your accessory view.