{"id":273,"date":"2008-01-09T13:09:17","date_gmt":"2008-01-09T21:09:17","guid":{"rendered":"http:\/\/www.corbinstreehouse.com\/blog\/?p=273"},"modified":"2018-10-31T10:15:16","modified_gmt":"2018-10-31T17:15:16","slug":"cocoa-willdisplaycell-delegate-method-of-nstableview-nscell-settextcolor-and-source-lists","status":"publish","type":"post","link":"https:\/\/www.corbinstreehouse.com\/blog\/2008\/01\/cocoa-willdisplaycell-delegate-method-of-nstableview-nscell-settextcolor-and-source-lists\/","title":{"rendered":"Cocoa: willDisplayCell delegate method of NSTableView, [NSCell setTextColor], and &#8220;source lists&#8221;"},"content":{"rendered":"<p id=\"top\" \/>\n<p>Mac OS 10.5 added a &#8220;source list&#8221; highlighting style to NSTableView, with the API below for your reference:<\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; color: #aa0d91\">enum <span style=\"color: #000000\">{<\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; color: #007400\"><span style=\"color: #2E0D6E;\">NSTableViewSelectionHighlightStyleRegular<\/span> <span style=\"color: #000000\">=<\/span> <span style=\"color: #1c00cf\">0<\/span><span style=\"color: #000000\">,<\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><span style=\"color: #2E0D6E;\">NSTableViewSelectionHighlightStyleSourceList<\/span> <span style=\"color: #000000\">=<\/span> <span style=\"color: #1c00cf\">1<\/span><span style=\"color: #000000\">,<\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco\">};<\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; color: #5c2699\"><span style=\"color: #aa0d91\">typedef<\/span> NSInteger NSTableViewSelectionHighlightStyle<span style=\"color: #000000\">;<\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px\"><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; color: #007400\"><span style=\"color: #000000\">&#8211; (<\/span>NSTableViewSelectionHighlightStyle<span style=\"color: #000000\">)selectionHighlightStyle;<\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco\">&#8211; (<span style=\"color: #aa0d91\">void<\/span>)setSelectionHighlightStyle:(<span style=\"color: #5c2699\">NSTableViewSelectionHighlightStyle<\/span>)selectionHighlightStyle;<\/p>\n<p><span style=\"font-family: Monaco; font-size: 10px;\"><br \/><\/span><\/p>\n<p>Source lists should have bold text when the item is selected, and NSTableView attempts to auto-format the cell&#8217;s contents to automatically do this for you, as seen in this screen shot for the selected item in the open panel source list:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/open_panel_selected_item_source_list2.png\" width=\"234\" height=\"60\" \/><\/p>\n<p>However, the code that does this formatting does so by converting the &#8216;stringValue&#8217; of the cell to an attributedStringValue that has the bold text. This is done *before* calling the delegate with &#8220;willDisplayCell&#8221;. The delegate gets the final say of how it looks in &#8220;willDisplayCell&#8221;, but this can cause unexpected results if you want to do something like this:<\/p>\n<p><span style=\"font-family: Monaco; font-size: 10px;\">&#8211; (<\/span><span style=\"color: #aa0d91\">void<\/span>)tableView:(<span style=\"color: #5c2699\">NSTableView<\/span> *)tableView willDisplayCell:(<span style=\"color: #aa0d91\">id<\/span>)cell forTableColumn:(<span style=\"color: #5c2699\">NSTableColumn<\/span> *)tableColumn row:(<span style=\"color: #5c2699\">NSInteger<\/span>)row {<\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco\"><span style=\"color: #aa0d91\">if<\/span>(row == <span style=\"color: #1c00cf\">0<\/span>) {<\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco\">[cell <span style=\"color: #2e0d6e\">setTextColor<\/span>: [<span style=\"color: #5c2699\">NSColor<\/span> <span style=\"color: #2e0d6e\">redColor<\/span>]];<\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco\">} <span style=\"color: #aa0d91\">else<\/span> {<\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; color: #2e0d6e\"><span style=\"color: #000000\">[cell<\/span> setTextColor<span style=\"color: #000000\">: [<\/span><span style=\"color: #5c2699\">NSColor<\/span> blackColor<span style=\"color: #000000\">]];<\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco\">}<\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco\">}<\/p>\n<p>The problem with the above code is that it is *too late* for the coloring to be correctly applied to the cell if it is selected. The work around is easy; you need to color the cell text earlier, and a perfect place to do that is in the new 10.5 delegate method &#8220;dataCellForTableColumn:&#8221;<\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco\">&#8211; (<span style=\"color: #5c2699\">NSCell<\/span> *)tableView:(<span style=\"color: #5c2699\">NSTableView<\/span> *)tableView dataCellForTableColumn:(<span style=\"color: #5c2699\">NSTableColumn<\/span> *)tableColumn row:(<span style=\"color: #5c2699\">NSInteger<\/span>)row {<\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco\"><span style=\"color: #5c2699\">NSTextFieldCell<\/span> *cell = [tableColumn <span style=\"color: #2e0d6e\">dataCell<\/span>];<\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco\"><span style=\"color: #aa0d91\">if<\/span>(row == <span style=\"color: #1c00cf\">0<\/span>) {<\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco\">[cell <span style=\"color: #2e0d6e\">setTextColor<\/span>: [<span style=\"color: #5c2699\">NSColor<\/span> <span style=\"color: #2e0d6e\">redColor<\/span>]];<\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco\">} <span style=\"color: #aa0d91\">else<\/span> {<\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; color: #2e0d6e\"><span style=\"color: #000000\">[cell<\/span> setTextColor<span style=\"color: #000000\">: [<\/span><span style=\"color: #5c2699\">NSColor<\/span> blackColor<span style=\"color: #000000\">]];<\/span><\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco\">}<\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco\"><span style=\"color: #aa0d91\">return<\/span> cell;<\/p>\n<p style=\"margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco\">}<\/p>\n<p>Cool. Have fun.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Mac OS 10.5 added a &#8220;source list&#8221; highlighting style to NSTableView, with the API below for your reference: enum { NSTableViewSelectionHighlightStyleRegular = 0, NSTableViewSelectionHighlightStyleSourceList = 1, }; typedef NSInteger NSTableViewSelectionHighlightStyle; &#8211; (NSTableViewSelectionHighlightStyle)selectionHighlightStyle; &#8211; (void)setSelectionHighlightStyle:(NSTableViewSelectionHighlightStyle)selectionHighlightStyle; Source&#8230; <a class=\"read-more\" href=\"https:\/\/www.corbinstreehouse.com\/blog\/2008\/01\/cocoa-willdisplaycell-delegate-method-of-nstableview-nscell-settextcolor-and-source-lists\/\">[read more]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4,6,86],"tags":[63,130,72],"class_list":["post-273","post","type-post","status-publish","format-standard","hentry","category-apple","category-cocoa","category-coding","tag-cocoa","tag-objc","tag-programming"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/posts\/273","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/comments?post=273"}],"version-history":[{"count":1,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/posts\/273\/revisions"}],"predecessor-version":[{"id":5178,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/posts\/273\/revisions\/5178"}],"wp:attachment":[{"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/media?parent=273"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/categories?post=273"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/tags?post=273"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}