I have NSTextField inside a View based NSTableView , I need to preserver first responder status after I do a [tableView reloadData] call . How can I do this ?
So far I have tried by setting the first responder during the following callback . But it does not have any effect.
(NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
Also I have tried by retrieving row after I reload the data and setting the first responder.
- (id)rowViewAtRow:(NSInteger)row makeIfNecessary:(BOOL)makeIfNecessary
Does not seems to work either .... Any help is appreciated . ..
Implement this method of NSTextfield:-
-(Bool)becomeFirstResponder
Related
I have an NSTableview, in this table view I populate columns with objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row.
I would like a column that is not populated. If I place a column with no identifier or an identifier that doesn't match a key I receive an error for not being key coding compliant.
Now what I have done to circumvent this is add a column with an identifier named 'skip', I check for this condition in my code with this...
-(id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{
Pizza* pizza = [pizza objectAtIndex:row];
NSString* identifier = [tableColumn identifier];
if ([identifier isEqualToString:#"skip"]) {
return nil;
}
return [pizza valueForKey:identifier];
}
I also have to reciprocate this in the setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row as well.
It seems to work fine, but it seems like a bit of a hack.
Is there a better way to do this?
Furthermore, the reason I want empty columns is because I want to place objects such as buttons that don't belong to the array that the table view is referencing.
Thanks for you input!
It's not a hack. That's how it's done. You're supposed to check the column identifier to figure out what object value to return for it. You wanted to just pass that choice through to the Pizza object, but it's common for a view to have aspects that aren't embodied in the model and therefore the controller has to mediate. If you want, you could override -valueForUndefinedKey: in the Pizza class but, in my opinion, that would be a hack. By the way, once you have a check of the column identifier, you don't have to use "skip". You can check for an empty identifier as easily.
I'm working with NSFetchedResultsController to fill one of my UITableViews with data grouped into sections. Now I want to add a new section atop all the fetched sections created by data that would not be fetched (I'm creating an additional array for that). Is there a nice way to insert that array without breaking all the nice NSFetchedResultsController behavior?
Update
What I've tried so far is to manually adjust every use of the indexPath. When ever a method (i.e. -tableView:numbersOfRowsInSection) get's called I check if it's the first section and if so I would take the data from my array instead of the NSFetchedResultsController. If not, I would just create a new NSIndexPath with the original indexPath's row and the section - 1 to access the fetchedResults. This seems to be a rather naive approach. At least, this does not work for me (yet).
Thanks!
–f
I created a UITableViewDataSource with a mixture of static and dynamic sections. I do it by modifying NSIndexPath like you planned. Important is to modify the NSIndexPath in the delegate methods of NSFetchedResultsControllerDelegate, too:
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
indexPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:kSectionWithAdditionalRowIndex];
if (newIndexPath) {
newIndexPath = [NSIndexPath indexPathForRow:newIndexPath.row + 1 inSection:kSectionWithAdditionalRowIndex];
}
[super controller:controller didChangeObject:anObject atIndexPath:indexPath forChangeType:type newIndexPath:newIndexPath];
}
The sample is taken from a subclass of my class CBUIFetchResultsDataSource.m, which is a generic UITableViewDataSource powered by a NSFetchedResultsController. I had to also overwrite -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section, -(id)objectAtIndexPath:(NSIndexPath*)indexPath and -(NSIndexPath*)indexPathForObject:(id)object.
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
I have create a NSTableView and 2 NSTableColumn in the view. Both of the identifiers of these 2 NSTableColumn set to "Automatic" in Interface Builder.
In the method:
(NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
I use
NSLog(#"%#", [tableColumn identifier])
to check the identifier of these 2 NSTableColumn.
I found the identifier of the first column is AutomaticTableColumnIdentifier.0 but the second is (null).
Is there any reason that the second column doesn't have a identifier?
I have reduced the attribute "Columns" of NSTableView to 1 and increased it to 2 later, so the first column is included in the NSTableView but the second is not.
This is likely the reason:
I have reduced the attribute "Columns" of NSTableView to 1 and increased it to 2 later, so the first column is included in the NSTableView but the second is not.
If it's causing you problems, set the identifiers in the nib.
I have NSTableView in my application. right now say is 8 columns by 48 rows.
I have a function that runs a specific a specific column to see wether or not the value in each cell is greater than a certain value. If it is I would like the application to highlight the row.
I did some reading and I am still looking for the functions calls or a process that will let me extract the cells/rows/or rect and let me change the color.
What are the functions and the steps in changing the color of the cells?
- [NSTableView selectRowIndexes:byExendingSelection:]
Source
To change the color of the cell you can try this method
-(void)tableView:(NSTableView *)tableView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
[cell setDrawsBackground:YES];
if(row==0)
[cell setBackgroundColor:[NSColor color];
else if(row==1||row==2)
[cell setBackgroundColor:[NSColor color];
else
[cell setBackgroundColor:[NSColor color];
}
this will make your row with different color.
Set a tableView delegate and implement the
– tableView:dataCellForTableColumn:row:
method. Then write a custom dataCell class to do the custom drawing.
If you extract the data as strings, you can use :-
NSAttributedString
to change the colour of both text and background.
-(void)tableView:(NSTableView *)tableView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
...gives you the pre-made cell (usually a NSTextFieldCell) which you can then color. Don't need to return it - Apple has already made it and is about to use it, just modify the object as you see fit. Works great.