Change text color in a table view

Hi,

is it possible to change the text color in a single data cell of a table view? Up to now I have found nothing in the documentations. Maybe has anyone an idea?

Heiner

Use the “will display cell” handler of the tableview/outlineview to evaluate each cell as it’s displayed. You can then set the text color of the cell before it is displayed if the default color is not appropriate.

Thank You Jobu for the reply,

but are you realy sure, that changing text color in a table cell is allowed?

I tried this:

on will display cell theObject cell theCell row theRow table column tableColumn set text color of data cell "sum" of data row theRow of theDataSource to {65535, 0, 0} -- red end will display cell
Result: Error message

Further more I want actualy change the color dynamicaly.
I tried this too:

on cell value changed theObject row theRow table column tableColumn value theValue if (contents of data cell "sum" of data row theRow of theDataSource begins with "5") or (contents of data cell "sum" of data row theRow of theDataSource is "6") then set text color of data cell "sum" of data row theRow of theDataSource to {65535, 0, 0} -- red else set text color of data cell "sum" of data row theRow of theDataSource to {0, 0, 0} -- black end if end cell value changed
Result: Error message

Did I make a mistake?

Heiner

Um, yes I’m sure. Here’s a basic example…

property MasterList : {{"Key 1", "Value 1"}, {"Key 2", "Value 2"}, {"Key 3", "Value 3"}}

on will open theObject
	tell table view "Table" of scroll view "Scroll" of theObject
		set content to MasterList
		update
	end tell
end will open

on will display cell theObject cell theCell row theRow table column tableColumn
	if (content of theCell is "Key 2") then
		if (highlighted of theCell is true) then
			set text color of theCell to {65500, 65500, 65500}
		else
			set text color of theCell to {65500, 0, 0}
		end if
	end if
end will display cell

Depending on how you provide your source data, you’ll need to establish a way of determining whether the text color in the cell needs to be modified or not. In this simple example, I test to see if the string in the cell matches a particular string and color the text accordingly. Note also that you need to provide an alternate color for when the row is highlighted, otherwise the text will always be the color you set it… which can make it hard to read a highlighted row. This handler is called a LOT, like every time the window resizes, the data changes, the scroll view scrolls, etc. Design your data source so you can extract the necessary information without extensive computation, or you may see a performance hit.

Hi jobu,

you are right, it’s possible to color a cell.
But … it costs a lot of time for updating the table (I’ ve 544 cells at maximum).
It seems to me, the ‘will display cell’-handler checks all cells after every event (even a simple mouseclick at the table).
I don’t know, how to shrink the numbers of cells for speed up the process. In my case, only the values of one column are important to change the color of the cells of the column aside.

???. I build my data source in an ordinary way (I think).

Heiner

Is there a way to change the color of a data cell’s text within a function without an “on will display” or “on cell value change”. I am running a function that checks if contents of data cell with tableView = false then set the text of that data cell to a different color. Any ideas?

Thanks!

Ok so this will change the color of every cell of every column but how do I change the color of an individual row?


set text color of every data cell of every table column of table view "servers" of scroll view "servers" of window "mainwindow" to {65535, 0, 0}

No one knows how to do this???

I am running into this issue as well “on will display” was killing my application and I had to pull it. Now my table has no color. :frowning:

Hey,

I’m also having a similar performance issues with this. I want to be able to colourise the second column on a certain row.

I’ve tried this, which does work, but is noticeably slow once you add something that matches “make_this_red”:

on will display cell theObject cell theCell row theRow table column tableColumn
	
	if tableColumn is table column 2 of table view "Table" of scroll view "View" of window "Window"then
		if (content of theCell is "make_this_red") then
			set text color of theCell to {65500, 0, 0}
		else
			set text color of theCell to {0, 0, 0}
		end if
	end if

end will display cell

So rather than do it that way I thought maybe I could set the colour afterwards instead of part of the ‘will display cell’ handler.

I tried this:

set text color of every data cell of table column 2 of table view "Table" of scroll view "View" of window "Window" to {65535, 0, 0}

as a test, which works resulting in the entire column of text turning red - with no performance issues manipulating the table view afterwards.

There are two problems though:

  1. I can’t seem to drill down any further to target a specific row in that column (either by row number or by the current selection).

  2. It means I’ll have to store which rows and columns have which text colour somehow so that it persists between launches, and if the rows are dragged about - this seems a lot easier to do if you manipulate the text colour during the ‘will display cell’ handler.

Anyway, I’m a bit stumped :frowning: If anyone can help I’d appreciate it!

I’ve noticed that even adding a new row that doesn’t match, or changing the data source in any way causes massive performance issues - I’m totally stumped why the performance issues wouldn’t be there from the start, or stop after you’ve finished manipulating the data source - I don’t think I know enough about the low level goings on here :frowning: