Tuesday, January 21, 2020

#1 2011-10-05 06:55:36 am

Heiner
Member
From:: Germany
Registered: 2007-08-25
Posts: 147

Text Color in Table View's Cell; Part II

As we discussed in a former topic how to coloring text in a table's cell the delegate tableView_willDisplayCell_forTableColumn_row_ works well.
Exept the count of data cells is not to much. Then the app hangs every time the user makes a key stroke.
An unsatisfactorily situation.

So I'm going to coloring the cells myself by a button (or menu item).
That led to a surprising result: I can read out the content of every cell but can't set the individual text color.

Applescript:


on bTableColor_(sender)
set rowCount to numberOfRowsInTableView_(aTableView)
set theColumn to aTableView's columnWithIdentifier_("myIdentifier")
aTableView's beginUpdates()
repeat with i from 1 to rowCount
set theCell to aTableView's preparedCellAtColumn_row_(theColumn,i-1)
set cellContent to theCell's stringValue()
if cellContent is in {"4", "3", "2", "1"} then
set aColor to myGreenColor --colorClass's greenColor
else
set aColor to colorClass's redColor
end if
log theCell -- always the same cell
log cellContent -- correct content
log aColor -- set correctly
theCell's setTextColor_(aColor)
end repeat
aTableView's endUpdates()
end bTableColor_

The result: the whole column is set to the color who is set at the end of the script.


Heiner

Last edited by Heiner (2011-10-05 09:10:06 am)

Offline

 

#2 2011-10-05 07:54:38 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11641
Website

Re: Text Color in Table View's Cell; Part II

Hi,

maybe reloadDataForRowIndexes:columnIndexes: of NSTableView could be a solution,
the method updates (redraws) only the specified rows/columns

Last edited by StefanK (2011-10-05 07:55:01 am)


regards

Stefan

Offline

 

#3 2011-10-05 09:08:01 am

Heiner
Member
From:: Germany
Registered: 2007-08-25
Posts: 147

Re: Text Color in Table View's Cell; Part II

Hi Stefan,

reloadDataForRowIndexes:columnIndexes: reloads the data as it says, so aTableView's reloadData() should do the same, but it doesn't. Only the whole column ist colored in the same color.
It's a pity!

Thanks,

Heiner

Offline

 

#4 2011-10-05 09:11:01 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11641
Website

Re: Text Color in Table View's Cell; Part II

Of course I mean  reloadDataForRowIndexes:columnIndexes: in conjunction with tableView:willDisplayCell:forTableColumn:row:


regards

Stefan

Offline

 

#5 2011-10-05 09:27:46 am

Heiner
Member
From:: Germany
Registered: 2007-08-25
Posts: 147

Re: Text Color in Table View's Cell; Part II

I don't know how it should work.
tableView:willDisplayCell:forTableColumn:row: is bound by the table as a delegate. The handler checks all cells in every column and every row in every table view (in my case there are three and ca. 400 cells for every table); that lasts for a very long time, even the handler is empty (only on and end).

Offline

 

#6 2011-10-05 09:37:00 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11641
Website

Re: Text Color in Table View's Cell; Part II

normally you send a reloadData() message to the table view which causes the whole table to be redrawn.
The reloadDataForRowIndexes:columnIndexes: method calls tableView:willDisplayCell:forTableColumn:row: only for the specified rows / columns which is much faster than redrawing the whole table


regards

Stefan

Offline

 

#7 2011-10-05 09:55:27 am

Heiner
Member
From:: Germany
Registered: 2007-08-25
Posts: 147

Re: Text Color in Table View's Cell; Part II

Sorry Stefan,
maybe I'm behind the moon.

In tableView_willDisplayCell_forTableColumn_row_(aTableView, acell, aColumn, aRow) is no need to reload the table. The 'system' do it permanently by itself. How can I have an Influence to that?

Offline

 

#8 2011-10-05 11:00:52 am

rdelmar
Member
From:: Sonoma County, CA
Registered: 2010-03-05
Posts: 1008

Re: Text Color in Table View's Cell; Part II

Would it be possible to use a completely different approach -- that is to have your data be NSAttributedStrings, so that the color would be an intrinsic part of the data instead of using the table view methods to provide the color.  This would depend on how you are getting the data into your table, and how you are using the data.  I'm sure it's possible this way, I've done it with a 50,000 row table, but it might not be the most efficient way depending on what you need to do with the data.

Ric

Offline

 

#9 2011-10-05 11:48:37 am

Heiner
Member
From:: Germany
Registered: 2007-08-25
Posts: 147

Re: Text Color in Table View's Cell; Part II

Would it be possible to use a completely different approach


Oh god!! My script has more than 5000 lines, the tables are communicating to each other and compare values. As I read in Doc's NSAttributedStrings  in rush comparison is not easy. My tables are handled via data sources and I think the new approach would strech my possibilities too far.
And: Coloring the cells in a different way would only be an additional feature in my app. It is not really necessary.

There are things between heaven and earth...

Thank you to both of you.

Heiner

Offline

 

#10 2011-10-05 04:33:11 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6143

Re: Text Color in Table View's Cell; Part II

Heiner,

You might want to look up "field editor" in the documentation, to see why you see what you see.

Another solution would be to use an NSValueTransformer. In fact, there's a project here homepage.mac.com/mmalc/CocoaExamples/controllers.html that shows how to do this in Objective-C (see the ToDos project).

If your requirements are simple, you should be able to edit the sample Objective-C code, rather than rewriting it in AS. That would help with speed significantly.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#11 2011-10-05 10:37:16 pm

rdelmar
Member
From:: Sonoma County, CA
Registered: 2010-03-05
Posts: 1008

Re: Text Color in Table View's Cell; Part II

That would help with speed significantly.


I tried out a transformer written in ASOC, and at least with my example (which converts numbers to attributed strings with colors that depend on the value of the number), and using bindings, the value transformer did not seem to take any time at all.  I created a half million member array of random numbers (that part I did do in Objective-C), that was used as the content for my table, and it only took 2 seconds to populate the table whether or not the value transformer was present. 

Ric

Offline

 

#12 2011-10-05 11:07:50 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6143

Re: Text Color in Table View's Cell; Part II

Interesting. That suggests a transformer is definitely worth investigating.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)