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.
Related
I have a NSTableCellView that has a simple NSTextField and an NSImageCell.
NSTableCellView *cell = [tableView makeViewWithIdentifier:#"List" owner:self];
cell.textField.stringValue = name;
cell.imageView.image = [NSImage imageNamed:#"image1.png"]; (or image 2,3, etc)
Later, when I select a row from the table, I'd like to be able to see the name of the image. If I preload the image from IB, I can use:
NSTableColumn *column = [self.tableView tableColumnWithIdentifier:#"List"];
NSCell *cell = [column dataCellForRow:row];
NSLog(#"TableView image:%#",cell.image.name);
This shows the IB Name - e.g. NSEveryWhere
But when I load the images at run time, the same statement results in (null).
Any help would be appreciated. Please note that this is for OS/X - however, for future use, I would appreciate iOS ideas as well. Thanks
i think you should use cell.imageView.image.name rather than logging cell.image.name
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
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 a view-based NSTableView that usually has one column in it. However, at a button press, I want a new column to slide in from the left (very similar to what happens on an iPhone when you click the Edit button in Mail). For now, the view that I want to slide is in a very simple view that draws a solid background: its drawRect: just does
[[NSColor blueColor] set];
[[NSBezierPath bezierPathWithRect:[self bounds]] fill];
In the delegate for my NSTableView, I have the following:
- (IBAction)buttonPressed:(id)sender
{
NSTableColumn *newColumn = [[NSTableColumn alloc] initWithIdentifier:#"InPreviewColumn"];
[newColumn setWidth:40];
[newColumn setMinWidth:[newColumn width]];
/* To make up for there not being an insertColumnAt: method,
hide the column, add it, and move it to the front before showing it. */
[newColumn setHidden:YES];
[availableFontsView beginUpdates];
[availableFontsView addTableColumn:newColumn];
[availableFontsView moveColumn:1 toColumn:0];
[availableFontsView endUpdates];
[newColumn setHidden:NO];
}
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
/* ... code for main column ... */
else if([[tableColumn identifier] isEqualToString:#"InPreviewColumn"])
{
USSolidBackgroundView *v = [tableView makeViewWithIdentifier:[tableColumn identifier] owner:self];
if(!v)
{
v = [[[USSolidBackgroundView alloc] initWithFrame:NSMakeRect(0, 0, [tableColumn width], 0)] autorelease];
[v setIdentifier:[tableColumn identifier]];
[v setAutoresizingMask:NSViewMinXMargin | NSViewWidthSizable | NSViewMaxXMargin];
}
return v;
}
}
Yet, when I do this, I end up with this result (there's a big non-blue margin between the blue part of the new column and the start of the main column):
When the additional column isn't present, there's no margin so I'm pretty sure that the problem isn't with the other view (since there's no margin when it's the only view displayed).
I've used logging statements to verify that solid color view's bounds always has a width of 40 (in its drawRect:) and, at least when the view is created, the table column has a width of 40 as well.
So where does this margin come from? No matter how I size the column, it seem that only roughly half of it is blue. So, the bigger the column, the bigger the margin.
How do I make the entire extra column blue?
The issue was my being stupid: In the view that draws the font names, I was basing the position on [self frame]. Which is wrong.
[self bounds] is the way to go. Which I knew. Can't believe I made this mistake. Amateur hour.
If you wander by this question, feel free to vote to close it or flag it or whatever it is that's supposed to work on Stackoverflow.
My apologies.
in my project I use a NSmutablearray that I fill with UIImageView in .m (viewDidLoad)
arrayView = [[NSMutableArray alloc] initWithObjects: image1, image2, image3, nil];
but in method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
when I write
int i = 1;
[[arrayView objectAtIndex:i] setImage:image];
there is an exception that say that my array is empty...why?
Please post the rest of viewDidLoad. I want to see how image1 is initialized. The line:
arrayView = [[NSMutableArray alloc] initWithObjects: image1, image2, image3, nil];
will return an empty array if image1 is nil. You should also protect your app from crashing by not making assumptions about the data source. You shouldn't crash if the array is empty, just display an empty tableView.
[EDIT]
I just read the last comment you made in the other answer. Sounds like your imageViews are created in IB. Make sure they are connected to the image1 etc outlets in IB.
Try this instead:
if (i < [arrayView count]) {
UIImageView *imageView = [arrayView objectAtIndex:i];
imageView.image = image;
}
Separating accessing the array element (by assigning it to an actual UIImageView object) and then assigning the new image may be helpful. I've seen cases where if you stack up too many operations things get confused, especially if you are dealing with objects of different types that may or may not have the selectors you're using.
Why your array is turning up empty is another issue. Initializing it in viewDidLoad seems right. You may need to add some "protection" (as above) in your table methods to avoid accessing an empty array. Then in method like viewWillAppear:, call reloadData.