{"id":431,"date":"2008-09-11T22:21:11","date_gmt":"2008-09-12T05:21:11","guid":{"rendered":"http:\/\/www.corbinstreehouse.com\/blog\/?p=431"},"modified":"2018-10-23T18:09:17","modified_gmt":"2018-10-24T01:09:17","slug":"why-the-api-nstableview-preparedcellatcolumnrow","status":"publish","type":"post","link":"https:\/\/www.corbinstreehouse.com\/blog\/2008\/09\/why-the-api-nstableview-preparedcellatcolumnrow\/","title":{"rendered":"Why the API? NSTableView -preparedCellAtColumn:row:"},"content":{"rendered":"<p id=\"top\" \/>I think I&#8217;ll do a few articles on why certain API was introduced in Leopard.<\/p>\n<p>I&#8217;ll start with one of the new methods in NSTableView:<\/p>\n<pre class=\"code\">\n\/* Returns the fully prepared cell that the view will normally use for\n drawing or any processing. The value for the cell will be correctly set, \n and the delegate method 'willDisplayCell:' will have be called. You can \n override this method to do any additional setting up of the cell that is \n required, or call it to retrieve a cell that will have its contents properly \n set for the particular column and row.\n*\/\n- (NSCell *)preparedCellAtColumn:(NSInteger)column row:(NSInteger)row;\n<\/pre>\n<p>What is this method and what is it useful for? There are many reasons why it is useful, and they all are based on this fact: *all* of NSTableView&#8217;s operations that involve a cell at a particular row\/column will be filtered through this method. Previously, NSTableView would have various internal methods and ways of obtaining the cell and there was no single spot where everything filtered through. Now there is.<\/p>\n<p>This means:<\/p>\n<p>1. You can acquire a &#8220;fully prepared&#8221; cell that is ready to draw &#8212; either in the NSTableView itself, or somewhere else. Somewhere else? Yes &#8212; an example where AppKit does this is for expansion tool tips, which draw in a separate tool tip window and in a view that is not actually an NSTableView. You might also want to access the &#8220;fully prepared&#8221; cell to generate a drag image when you override:<\/p>\n<pre class=\"code\">\n- (NSImage *)dragImageForRowsWithIndexes:(NSIndexSet *)dragRows \n\ttableColumns:(NSArray *)tableColumns \n\tevent:(NSEvent*)dragEvent \n\toffset:(NSPointPointer)dragImageOffset;\n<\/pre>\n<p>2. You can override the method to modify the cell. Think about this in a simple way, and it means you could easily bold odd rows without having to use -willDisplayCell:. Depending on how your code is setup, it might make more sense to do this work in the view, as opposed to the controller (via the delegate).<\/p>\n<p>3. You can return an entirely different cell. Let&#8217;s say you want to have one cell that doesn&#8217;t change for one particular row; well, you can just always return that same cell, and it will always be used. You might want to do this if that particular cell has some sort of important state (such as an animation), which you don&#8217;t want to have to keep resetting or managing. Another example is for showing highlighting when you mouse over the cell. <\/p>\n<p>Take this example from WWDC that I wrote a few years ago: <a href=\"http:\/\/developer.apple.com\/samplecode\/PhotoSearch\/index.html\">PhotoSearch<\/a>. Look at <a href=\"http:\/\/developer.apple.com\/samplecode\/PhotoSearch\/listing21.html\">TrackableOutlineView.m<\/a>, and you&#8217;ll see that the cell which is being tracked (ie: mouse is over it), is always returned from preparedCellAtColumn:row:. This was important from an abstraction perspective; the cell will get a single -mouseEntered: from the tracking area, and the same cell will eventually get a -mouseExited: &#8212; this allows the cell to set and maintain its own state, without forcing the delegate or NSTableView to save off the state and always reset it at draw time. <\/p>\n<p>Why does this demo copy the cell, and not just use the single cell that is shared from the [tableColumn dataCell]? I mean, if the cell gets a mouseEntered:, and the tableView redraws *just* that cell, then things will work out fine, right? Yes &#8212; but if something else triggers any other row to redraw, then your tracked cell will be used, and it currently has the wrong state in it. That would be bad, and it would draw the wrong thing.<\/p>\n<p>4. Another reason this method is useful is for type selection. When you type select, and NSTableView is attempting to find a row, it can acquire a fully prepared cell (including setting the stringValue, calling -willDisplayCell on the delegate, and setting up bindings). The stringValue of the cell can then be compared for type-selection to find a match. But, you wonder, isn&#8217;t that doing too much work for just matching on the string? Yes &#8212; for any decent sized table, you should implement this delegate method to directly return information from your model:<\/p>\n<pre>\n- (NSString *)tableView:(NSTableView *)tableView \n\ttypeSelectStringForTableColumn:(NSTableColumn *)tableColumn \n\trow:(NSInteger)row;\n<\/pre>\n<p>5. Finally, another reason for the API; there is no easy way to know what the value of a cell will be if you are using bindings. Now, you can find out by using this method, since it will fill the cell&#8217;s content with bound values (if there are any).<\/p>\n<p>Anyways, my first entry of &#8220;Why the API&#8221;. More to follow&#8230;hopefully.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I think I&#8217;ll do a few articles on why certain API was introduced in Leopard. I&#8217;ll start with one of the new methods in NSTableView: \/* Returns the fully prepared cell that the view will&#8230; <a class=\"read-more\" href=\"https:\/\/www.corbinstreehouse.com\/blog\/2008\/09\/why-the-api-nstableview-preparedcellatcolumnrow\/\">[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":[72],"class_list":["post-431","post","type-post","status-publish","format-standard","hentry","category-apple","category-cocoa","category-coding","tag-programming"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/posts\/431","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=431"}],"version-history":[{"count":1,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/posts\/431\/revisions"}],"predecessor-version":[{"id":5172,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/posts\/431\/revisions\/5172"}],"wp:attachment":[{"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/media?parent=431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/categories?post=431"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/tags?post=431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}