<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"><channel><title>mikeash.com pyblog/custom-nscells-done-right.html comments</title><link>http://www.mikeash.com/?page=pyblog/custom-nscells-done-right.html#comments</link><description>mikeash.com Recent Comments</description><lastBuildDate>Mon, 17 Aug 2026 11:27:26 GMT</lastBuildDate><generator>PyRSS2Gen-1.0.0</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>k06a - 2015-08-16 05:25:07</title><link>http://www.mikeash.com/?page=pyblog/custom-nscells-done-right.html#comments</link><description>Aaron Smith, nice implementation, improved it a little bit to work with sub-sub-classes:
&lt;br /&gt;&lt;a href="https://gist.github.com/k06a/3e8703538d9c4063cf04"&gt;https://gist.github.com/k06a/3e8703538d9c4063cf04&lt;/a&gt;</description><guid isPermaLink="true">83c9f481d78ac9bf751d2752504410ad</guid><pubDate>Sun, 16 Aug 2015 05:25:07 GMT</pubDate></item><item><title>k06a - 2015-07-23 12:30:48</title><link>http://www.mikeash.com/?page=pyblog/custom-nscells-done-right.html#comments</link><description>Please read doc carefully: &lt;a href="https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ControlCell/Tasks/SubclassingNSControl.html#//apple_ref/doc/uid/20000730"&gt;https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ControlCell/Tasks/SubclassingNSControl.html#//apple_ref/doc/uid/20000730&lt;/a&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;div class="blogcommentquote"&gt;&lt;div class="blogcommentquoteinner"&gt;Note that overriding the cellClass class method of NSControl does not change the class of a cell object unarchived from a nib file.&lt;/div&gt;&lt;/div&gt;</description><guid isPermaLink="true">504fd08f889b01fef558bcec2cfda343</guid><pubDate>Thu, 23 Jul 2015 12:30:48 GMT</pubDate></item><item><title>Mark Aufflick - 2014-05-23 06:37:52</title><link>http://www.mikeash.com/?page=pyblog/custom-nscells-done-right.html#comments</link><description>@Aaron you're going to want a dispatch_once wrapped around that swizzling code in initialize. You also don't need to call initialize on super, the runtime will call it on all classes/subclasses as per the docs.</description><guid isPermaLink="true">d9a50ecbb9bda60b1cc467e6dfb74a8e</guid><pubDate>Fri, 23 May 2014 06:37:52 GMT</pubDate></item><item><title>Aaron Smith - 2010-05-05 19:30:58</title><link>http://www.mikeash.com/?page=pyblog/custom-nscells-done-right.html#comments</link><description>here's an updated version that will work with new 10.X SDK's..
&lt;br /&gt;
&lt;br /&gt;Put this code in a category addition to NSControl
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;@implementation NSControl (Additions)
&lt;br /&gt;
&lt;br /&gt;+ (void) initialize {
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[[[self class] superclass] initialize];
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Class NSControlClass = [NSControl class];
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;method_exchangeImplementations(class_getInstanceMethod(NSControlClass,@selector(initWithCoder:)),class_getInstanceMethod(NSControlClass,@selector(newInitWithCoder:)));
&lt;br /&gt;}
&lt;br /&gt;
&lt;br /&gt;//inspired from: [h]ttp://&lt;a href="http://www.mikeash.com/pyblog/custom-nscells-done-right.html"&gt;www.mikeash.com/pyblog/custom-nscells-done-right.html&lt;/a&gt;
&lt;br /&gt;- (id) newInitWithCoder:(NSCoder *) _coder {
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(![_coder isKindOfClass:[NSKeyedUnarchiver class]]) return [self newInitWithCoder:_coder];
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;NSKeyedUnarchiver * unarchiver = (NSKeyedUnarchiver *)_coder;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Class supercell = [[self superclass] cellClass];
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Class selfcell = [[self class] cellClass];
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(!selfcell || !supercell) return [self newInitWithCoder:_coder];
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;NSString * supercellName = NSStringFromClass(supercell);
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[unarchiver setClass:selfcell forClassName:supercellName];
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;self = [self newInitWithCoder:_coder];
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[unarchiver setClass:supercell forClassName:supercellName];
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return self;
&lt;br /&gt;}
&lt;br /&gt;
&lt;br /&gt;@end
&lt;br /&gt;&lt;/code&gt;</description><guid isPermaLink="true">e39aadeefa62a036b548c32f85d7f132</guid><pubDate>Wed, 05 May 2010 19:30:58 GMT</pubDate></item><item><title>Hendo - 2007-08-12 13:33:00</title><link>http://www.mikeash.com/?page=pyblog/custom-nscells-done-right.html#comments</link><description>Hi, I&amp;amp;#8217;m new to Obj-C, and I just need a little help understanding how to use this. I went into InterfaceBuilder and created a subclass of NSControl called FixedNSControl. I assume I was then supposed to add the .m and .h files to my project? If thats correct what should the code look like in each file? Thanks!&amp;lt;br /&amp;gt;
&lt;br /&gt;&amp;nbsp;&amp;amp;#8211; Hendo
&lt;br /&gt;</description><guid isPermaLink="true">dfc65a1dfd8867b35ec9c99800dabd71</guid><pubDate>Sun, 12 Aug 2007 13:33:00 GMT</pubDate></item><item><title>Dave MacLachlan - 2007-04-18 17:13:00</title><link>http://www.mikeash.com/?page=pyblog/custom-nscells-done-right.html#comments</link><description>Just as a note:&amp;lt;br /&amp;gt;
&lt;br /&gt;&amp;lt;br /&amp;gt;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;We were using a similar method, and ran into a &lt;b&gt;very&lt;/b&gt; interesting issue. If you poseAsClass for NSControl, and you launch your app from the postflight stage of an installer, you &lt;b&gt;will&lt;/b&gt; not be able to paste into any text fields. Strange but true. Remove the poseAsClass and everything works fine. Launch your app from the Finder and it&amp;amp;#8217;s all good. Logged as radar: &lt;i&gt;5142672 Installer/PoseAs combo cause paste not to work&lt;/i&gt;
&lt;br /&gt;</description><guid isPermaLink="true">ed6ddd0c3c67e34b75e120e6d666bd7d</guid><pubDate>Wed, 18 Apr 2007 17:13:00 GMT</pubDate></item><item><title>mikeash - 2007-01-17 20:43:00</title><link>http://www.mikeash.com/?page=pyblog/custom-nscells-done-right.html#comments</link><description>You do understand the difference between an open standard with a human-readable format and a closed standard that is intimately tied to a single application framework, right?&amp;lt;br /&amp;gt;
&lt;br /&gt;&amp;lt;br /&amp;gt;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Call me back when there&amp;amp;#8217;s a nib editor other than Interface Builder, or when Cocoa offers another format for interface layout than nibs. IB has been an integral part of Cocoa development since it was NeXTSTEP. The situation is so hugely different from the HTML/Notepad situation that your analogy cannot possibly be thought to have any relevance.
&lt;br /&gt;</description><guid isPermaLink="true">1a0e38032f67f14997fdaf29ecdbe02f</guid><pubDate>Wed, 17 Jan 2007 20:43:00 GMT</pubDate></item><item><title>dude - 2007-01-16 17:00:00</title><link>http://www.mikeash.com/?page=pyblog/custom-nscells-done-right.html#comments</link><description>That&amp;amp;#8217;s like saying CSS and HTML suck because Notepad.exe blows. Don&amp;amp;#8217;t blame the API when it&amp;amp;#8217;s the &amp;lt;strong&amp;gt;tool&amp;lt;/strong&amp;gt; that&amp;amp;#8217;s broken. You do understand the difference, right?
&lt;br /&gt;</description><guid isPermaLink="true">1b4739fbfa556325632dfff1e3cce2e7</guid><pubDate>Tue, 16 Jan 2007 17:00:00 GMT</pubDate></item><item><title>mikeash - 2007-01-14 03:44:00</title><link>http://www.mikeash.com/?page=pyblog/custom-nscells-done-right.html#comments</link><description>As Interface Builder is part of the Cocoa development system, it&amp;amp;#8217;s entirely fair to state that Cocoa makes it difficult.
&lt;br /&gt;</description><guid isPermaLink="true">10ad97272d81276fe73e5ae611866d0d</guid><pubDate>Sun, 14 Jan 2007 03:44:00 GMT</pubDate></item><item><title>dude - 2007-01-13 08:12:00</title><link>http://www.mikeash.com/?page=pyblog/custom-nscells-done-right.html#comments</link><description>&amp;amp;#8220;Wow, thank you thank you thank you! Today I attempted to subclass NSButtonCell for the first time, and wasted a good many hours before I found your solution. I cant believe Cocoa makes this task so difficult&amp;amp;#8221;&amp;lt;br /&amp;gt;
&lt;br /&gt;&amp;lt;br /&amp;gt;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Cocoa doesn&amp;amp;#8217;t make it difficult; this is a deficiency in Interface Builder.
&lt;br /&gt;</description><guid isPermaLink="true">b82ba391e3f86ba9e9bac5c3a54d6d25</guid><pubDate>Sat, 13 Jan 2007 08:12:00 GMT</pubDate></item><item><title>mikeash - 2006-10-23 05:22:00</title><link>http://www.mikeash.com/?page=pyblog/custom-nscells-done-right.html#comments</link><description>Your specific examples work, but you can&amp;amp;#8217;t generalize them. For example, using setCellClass: on a subclassed NSButton that&amp;amp;#8217;s created from a regular NSButton in IB will do absolutely nothing at all. Using custom cells in tables has always been easy, the trouble is using custom cells in controls that are not so enlightened.
&lt;br /&gt;</description><guid isPermaLink="true">b3005b418ac361ca5ac7c0a2cf998fff</guid><pubDate>Mon, 23 Oct 2006 05:22:00 GMT</pubDate></item><item><title>Jop van Heesch - 2006-10-22 11:38:00</title><link>http://www.mikeash.com/?page=pyblog/custom-nscells-done-right.html#comments</link><description>Maybe I am missing the point, but why not do the following?&amp;lt;br /&amp;gt;
&lt;br /&gt;&amp;lt;br /&amp;gt;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;If you want to use your own control: call setCellClass: in the control&amp;amp;#8217;s initialize method.&amp;lt;br /&amp;gt;
&lt;br /&gt;if you use a control declared in IB: set the cell in awakeFromNib, e.g. with NSTableColumn&amp;amp;#8217;s setDataCell:
&lt;br /&gt;</description><guid isPermaLink="true">7b6b6edecd261d4f921e60e1b1ee5d26</guid><pubDate>Sun, 22 Oct 2006 11:38:00 GMT</pubDate></item><item><title>Daniel Dickison - 2006-09-14 04:03:00</title><link>http://www.mikeash.com/?page=pyblog/custom-nscells-done-right.html#comments</link><description>I found a minor bug in this code: if you are using a custom sub-sub-class of a NSControl, then the following line doesn&amp;amp;#8217;t work:&amp;lt;br /&amp;gt;
&lt;br /&gt;sub = sub &amp;amp;#38;&amp;amp;#38; [[self superclass] cellClass] != [[self class] cellClass]; // pointless if same&amp;lt;br /&amp;gt;
&lt;br /&gt;&amp;lt;br /&amp;gt;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;I posted a fix along with a demo project &amp;lt;a href="&lt;a href="http://danieldickison.com/blog/index.php?/archives/10-Making-Cocoa-buttons-look-happy.html"&gt;http://danieldickison.com/blog/index.php?/archives/10-Making-Cocoa-buttons-look-happy.html&lt;/a&gt;" rel="nofollow"&amp;gt;here&amp;lt;/a&amp;gt;
&lt;br /&gt;</description><guid isPermaLink="true">23af67d0e423ab6cfd065ee82f71fdee</guid><pubDate>Thu, 14 Sep 2006 04:03:00 GMT</pubDate></item><item><title>Daniel Dickison - 2006-09-13 22:31:00</title><link>http://www.mikeash.com/?page=pyblog/custom-nscells-done-right.html#comments</link><description>Wow, thank you thank you thank you!  Today I attempted to subclass NSButtonCell for the first time, and wasted a good many hours before I found your solution.  I can&amp;amp;#8217;t believe Cocoa makes this task so difficult&amp;amp;#8230;
&lt;br /&gt;</description><guid isPermaLink="true">55390ae88698279662d96eb1c9012729</guid><pubDate>Wed, 13 Sep 2006 22:31:00 GMT</pubDate></item><item><title>Robert McCullough - 2006-06-15 17:08:00</title><link>http://www.mikeash.com/?page=pyblog/custom-nscells-done-right.html#comments</link><description>Hi Mike,&amp;lt;br /&amp;gt;
&lt;br /&gt;&amp;lt;br /&amp;gt;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Great class! I&amp;amp;#8217;m having to create a bunch of custom controls and this was exactly what I&amp;amp;#8217;m looking for. I found a small problem with NSComboBoxes though. Your initWithCoder method was substituting a NSComboBoxCell class for a NSTextFieldCell class in a combo box object (which led to some pretty strange looking combo boxes).&amp;lt;br /&amp;gt;
&lt;br /&gt;&amp;lt;br /&amp;gt;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;I was able to solve the problem by adding this additional check to initWithCoder:&amp;lt;br /&amp;gt;
&lt;br /&gt;&amp;lt;br /&amp;gt;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;sub = sub &amp;amp;#38;&amp;amp;#38; ([NSStringFromClass( [[self class] cellClass]) hasPrefix:@&amp;amp;#8221;NS&amp;amp;#8221;] == NO);&amp;lt;br /&amp;gt;
&lt;br /&gt;&amp;lt;br /&amp;gt;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;This assumes that we only want to make the substitution with one of our custom cell classes (which shouldn&amp;amp;#8217;t start with &amp;amp;#8220;NS&amp;amp;#8221&amp;lt;img src="/blog/pivot/includes/emot/e_121.gif" alt=";)" align="middle" /&amp;gt;.&amp;lt;br /&amp;gt;
&lt;br /&gt;&amp;nbsp;&amp;amp;#8211; Rob
&lt;br /&gt;</description><guid isPermaLink="true">cb6625dd028a924c480f148e75cd65cf</guid><pubDate>Thu, 15 Jun 2006 17:08:00 GMT</pubDate></item></channel></rss>
