NSTableView - NSButtonCell Data Source type? - cocoa

I've got a table with checkbox-style cells, and I can't figure out how to get these buttons to take on the titles that they're supposed to. Should the data source be an array of strings? An array of dictionaries (string/boolean)? An array of NSButtonCells? None of these seem to work =/

NSButtonCell uses integer values (as NSNumbers) as its data source:
NSMixedState = -1,
NSOffState = 0,
NSOnState = 1
That doesn't help you with the title of course, you have to set that separately. If you're using bindings, NSButtonCell defines a title binding you can bind to an array of strings. Otherwise, you can use the NSTableView delegate method - (void)tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex to set the title for each row.

Related

Reordering cells in a PFQueryTableView?

How do you handle reordering of cells in a PFQueryTableView if the self.objects array is readonly?
I Can get the table to enter into editing mode and I can even rearrange the cells, but I don't know how to handle the change in the delegate method. I tried creating an NSMutableArray from the self.objects array but I don't know to update the objects in parse. This is what I have tried:
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
NSMutableArray *array = [[NSMutableArray alloc]initWithArray:self.objects];
PFObject *movedObject = array[fromIndexPath.row];
[array removeObjectAtIndex:fromIndexPath.row];
[array insertObject:movedObject atIndex:toIndexPath.row];
//Then trying to reassign the updated mutable array to the self.objects array is not possible because its read only.
}
Any help would be greatly appreciated.
After much though I realized that the way to handle rearranging of cells with parse objects is individually updating the rearranged objects and not by trying to create an updated array.

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

Sorting NSTableView

I have two coloumn in NSTableView as Name and Salary with 5-10 values. I want to sort these coloumn after click on header of both the column. There is lots of data present in Internet but I am not able to use these. Please help me to do this in cocoa.
Thanks in advance and appreciate any help.
Each table column has a method setSortDescriptorPrototype
Sort descriptors are ways of telling the array how to sort itself (ascending, descending, ignoring case etc.)
Iterate over each of the columns you want as sortable and call this method on each of those columns, and pass the required sort descriptor (In my case I'll be using the column identifier)
for (NSTableColumn *tableColumn in tableView.tableColumns ) {
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:tableColumn.identifier ascending:YES selector:#selector(compare:)];
[tableColumn setSortDescriptorPrototype:sortDescriptor];
}
After writing this piece of initialization code, NSTableViewDataSource has a method - (void)tableView:(NSTableView *)aTableView sortDescriptorsDidChange:(NSArray *)oldDescriptors that notifies you whenever a sort descriptor is changed, implement this method in the data source and send a message to the data array to sort itself
- (void)tableView:(NSTableView *)aTableView sortDescriptorsDidChange:(NSArray *)oldDescriptors
{
self.data = [self.data sortedArrayUsingDescriptors:sortDescriptors];
[aTableView reloadData];
}
This method will get fired each time a column header is clicked, and NSTableColumn shows a nice little triangle showing the sorting order.
I stumbled upon this question while looking for the easiest way of implementing something similar. Although the original question is old, I hope someone finds my answer useful! Please note that I am using Xcode 5.1.1
Ok so to do this you need to:
select the actual column you want to sort in your table.
In your Attributes Inspector you need to fill in two fields: Sort Key, and Selector.
In the Sort Key field, you need to enter the value of your Identifier. The value of your Identifier is located in your Identity Inspector.
In the Selector field you need to enter a suitable selector method based on the object type in the column. The default method is; compare:
Based on the Table View Programming Guide for Mac. The compare: method works with NSString, NSDate, and NSNumber objects. If your table column contains only strings, you may want to consider using the caseInsensitiveCompare: method if case sensitivity is unimportant. However, consider replacing these method signatures with the localizedCompare: or localizedCaseInsensitiveCompare: methods to take into the account the user’s language requirements.
Finally, you need to declare the tableView:sortDescriptorsDidChange: method in your Table View Controller in the format shown below:
-(void)tableView:(NSTableView *)mtableView sortDescriptorsDidChange:(NSArray *)oldDescriptors
{
[listArray sortUsingDescriptors: [mtableView sortDescriptors]];
[tableView reloadData];
}
Just had lately the same issue to get tableView sorted.
My approach :
bind your sortDescriptors to tableview's arrayController
bind tableview's sortDescriptors to Arraycontroller's sort descriptor
perform the settings in attribute inspector (see Tosin's answer above)
Worked perfect for me. No need to set prototypes for columns or something else.
Thanks very much ,It is usefullly for my question.
my code like this
First, set unique values in the XIB interface,like name...
- (void)viewDidLoad {
[super viewDidLoad];
self.itemTableView.dataSource = self;
self.itemTableView.delegate = self;
self.itemTableView.selectionHighlightStyle = NSTableViewSelectionHighlightStyleRegular;
self.itemTableView.usesAlternatingRowBackgroundColors = YES;
for (NSTableColumn *tableColumn in self.itemTableView.tableColumns ) {
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:tableColumn.identifier ascending:NO selector:#selector(compare:)];
[tableColumn setSortDescriptorPrototype:sortDescriptor];
}
}
-(void)tableView:(NSTableView *)tableView sortDescriptorsDidChange:(NSArray<NSSortDescriptor *> *)oldDescriptors{
NSLog(#"sortDescriptorsDidChange:%#",oldDescriptors);
[self.itemArr sortUsingDescriptors:[tableView sortDescriptors]];
[self.itemTableView reloadData];
}

Binding to NSTextField Cell not working, edited value resets to default

I'm working on a Core Data document application that dynamically creates NSTableColumns. The data cell type may be a checkbox, slider, etc. Programmatically binding to all cell types works, except for NSTextFieldCell.
All NSTextFieldCells fail to bind, and after editing they return to their default value. This happens no matter if they're binding to a string, a number (with an NSNumberFormatter applied), or a date (NSDateFormatter applied). I'm using the following format to do all bindings:
NSDictionary *textFieldOpts = [NSDictionary dictionaryWithObjectsAndKeys:#"YES", NSContinuouslyUpdatesValueBindingOption, #"YES", NSValidatesImmediatelyBindingOption, nil];
[aCell bind:#"value" toObject:[[entryAC arrangedObjects] objectAtIndex:0] withKeyPath:#"numberData" options:textFieldOpts];
Again, these statements work if the cell type is anything but an NSTextFieldCell.
I threw in an -observeValueForKeyPath method to log when the value changes... and for other cell types (NSSliderCell for instance) I can see the value changing, but with the NSTextFieldCell, it never, ever updates.
Turns out that I needed to implement the NSTableView data source method setObjectValue to manually get the change from the NSTableView (View) and then manually set the data in the array controller (Model) similar to the code below:
- (void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex {
[[[entryAC arrangedObjects] objectAtIndex:rowIndex] setValue:(NSNumber *)anObject forKey:#"numberData"];
}
For some reason, the bindings to NSTextFieldCells, when set programmatically on a cell-by-cell basis, only work one way: to display the data only. Personally I would classify this as a bug...

NSTableView and displaying sort image in column header question

I have a NSTableView with 3 columns. I can sort using any of the colunms by clicking on the column header. Here is my issue though: I am presorting the array and then loading it in NSTableView so the initial TableView is already sorted by the values in one of the 3 columns. Now when I click on the column headers, i can resort and the small ascending/descending image ( triangle) appears indicating how the sort order is. What I want is, to be able to display this ascendingimage triangle in the column header right at the start when the NSTableView is loaded the first time, for the column based on which I have already sorted the Array. Thanks in advance :)
Thanks Peter and cb160. So I added this in my refreshList method:
The *lastColumn parameter has the right identifier if i display it using NSLog, but still that triangle image does not get loaded when the table loads data the first time. Is there something I am missing here ?
My table View is setup like this:
-(IBAction)refreshList:(id)sender
{
//setup array here and sort the array based on one column. This column has
identifier 'col1' and it works as expected
// trying to set the sortindicator image for the column here
NSTableColumn *lastColumn;
lastColumn = [aTableView tableColumnWithIdentifier:#"col1"];
[aTableView setIndicatorImage:[NSImage imageNamed:#"NSDescendingSortIndicator"]
inTableColumn:lastColumn];
[aTable reloadData];
}
- (int) numberOfRowsInTableView:(NSTableView *)aTable
{ // return count of array
}
- (id)tableView:(NSTableView *)aTable objectValueForTableColumn: (NSTableColumn *)
tableColumn row:(int)row
{
//set up arrays here to load data in each column
}
- (void)tableView:(NSTableView *)aTableView sortDescriptorsDidChange:(NSArray
*)oldDescriptors
{
//sort here when column headers are clicked
}
You can use the -setIndicatorImage:inTableColumn: method on NSTableView to do this.
You can use the strings NSAscendingSortIndicator and NSDescendingSortIndicator to use the built in images (with UIImage's +imageNamed: method.)
You can get the NSTableColumn * parameter for this method using the -tableColumnWithIdentifier: method on NSTableView. Set the idenitfier in the column using the identifier attribute in Interface Builder (see below)

Resources