{"id":274,"date":"2007-02-09T10:31:52","date_gmt":"2007-02-09T17:31:52","guid":{"rendered":"http:\/\/www.red-sweater.com\/blog\/274\/a-moveable-beast"},"modified":"2007-02-09T22:27:56","modified_gmt":"2007-02-10T05:27:56","slug":"a-moveable-beast","status":"publish","type":"post","link":"https:\/\/redsweater.com\/blog\/274\/a-moveable-beast","title":{"rendered":"A Moveable Beast"},"content":{"rendered":"<p>Implementing reordering table views is something of a first-time obstacle in Cocoa. The basic gist is you take responsibility for dragging <em>from<\/em> and <em>to<\/em> the table view in question, and thereby implicitly support drag reordering. This technique is demonstrated by Apple&#8217;s <a href=\"file:\/\/localhost\/Developer\/Examples\/AppKit\/DragNDropOutlineView\/\">DragNDropOutlineView<\/a> example.<\/p>\n<p>\nFaced with the desire to implement such functionality in a bindings-oriented application (<a href=\"http:\/\/www.red-sweater.com\/flextime\/\">FlexTime<\/a>), I realized it would make sense to have the dragging handled by a the NSArrayController, which is already handling so much of the behavior associated with the table view.\n<\/p>\n<p>\n<a href=\"http:\/\/www.red-sweater.com\/blog\/downloads\/RSRTVArrayController.zip\">RSRTVArrayController<\/a> implements a subclass of NSArrayController whose responsibilities are extended to provide simple row reordering via Drag-and-Drop. The basic gist is:\n<\/p>\n<ol>\n<li>Drop this one class into your project.<\/li>\n<li>Drag the header file to Interface Builder<\/li>\n<li>Set it as the custom class on an array controller instance<\/li>\n<li>Connect the dataSource and delegate outlets from the table view to the controller.<\/li>\n<li>Connect the oTableView outlet from the controller to the table view.<\/li>\n<\/ol>\n<p>\n Et voila!\n<\/p>\n<p>\nYou can also temporarily disable reordering by calling &#8220;setDraggingEnabled:&#8221; on the controller. I do this for instance in FlexTime while the routine is running so users can&#8217;t completely confuse my world.<\/p>\n<p>\n<strong>Update:<\/strong> <a href=\"http:\/\/centrix.ca\/\">Rick Fillion<\/a> pointed out a possible day-ruining bug in the code, which will only rear it&#8217;s head if you happen to be assigning all ownership of your model objects to the array controller. Since the &#8220;move&#8221; code accomplishes the move by removing and then inserting an object from the array, it turns out that once it&#8217;s removed, it&#8217;s deallocated if nothing else in your app is retaining it!  So the workaround is to be retain the object while moving it, and then release it. Thanks for reporting that, Rick. The downloadable class is now updated.\n<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Implementing reordering table views is something of a first-time obstacle in Cocoa. The basic gist is you take responsibility for dragging from and to the table view in question, and thereby implicitly support drag reordering. This technique is demonstrated by Apple&#8217;s DragNDropOutlineView example. Faced with the desire to implement such functionality in a bindings-oriented application [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,29,15],"tags":[],"class_list":["post-274","post","type-post","status-publish","format-standard","hentry","category-cocoa","category-free-code","category-programming"],"_links":{"self":[{"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/posts\/274","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/comments?post=274"}],"version-history":[{"count":0,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/posts\/274\/revisions"}],"wp:attachment":[{"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/media?parent=274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/categories?post=274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/tags?post=274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}