{"id":3631,"date":"2014-04-25T13:23:15","date_gmt":"2014-04-25T21:23:15","guid":{"rendered":"http:\/\/www.corbinstreehouse.com\/blog\/2014\/04\/implementing-delete-in-an-nstableview\/"},"modified":"2018-11-03T13:36:24","modified_gmt":"2018-11-03T20:36:24","slug":"implementing-delete-in-an-nstableview","status":"publish","type":"post","link":"https:\/\/www.corbinstreehouse.com\/blog\/2014\/04\/implementing-delete-in-an-nstableview\/","title":{"rendered":"Implementing delete in an NSTableView"},"content":{"rendered":"<p id=\"top\" \/>\n<p>I&#8217;ve seen many ways to implement delete in an NSTableView. Many are good. Many hardcode references to something they shouldn&#8217;t, and those are bad.<\/p>\n<p>Here&#8217;s an easy way:<\/p>\n<p>Subclass NSTableView and override keyDown:<\/p>\n<p style=\"margin: 0px; font-size: 11px; font-family: Menlo;\">\n<pre><tt>\r\n- (void)keyDown:(NSEvent *)theEvent {\r\n    \/\/ handle delete\r\n    unichar key = [[theEvent charactersIgnoringModifiers] characterAtIndex:0];\r\n    if (key == NSDeleteCharacter && self.selectedRow != -1) {\r\n        [NSApp sendAction:@selector(delete:) to:nil from:self];\r\n    } else {\r\n      [super keyDown:theEvent];\r\n  }\r\n}\r\n<\/tt><\/pre>\n<p style=\"margin: 0px; font-size: 11px; font-family: Menlo;\"><\/p>\n<p style=\"margin: 0px; font-size: 11px; font-family: Menlo;\"><span style=\"font-family: Helvetica; font-size: 12px;\">This just lets the responder chain take care of it. Your window controller can easily handle it by just implementing delete:<\/span><\/p>\n<p style=\"margin: 0px; font-size: 11px; font-family: Menlo;\"><\/p>\n<p style=\"margin: 0px;\">\n<p style=\"margin: 0px;\">\n<pre>\r\n- (IBAction)delete:(id)sender {\r\n    if (self.tableView.selectedRowIndexes.count > 0) {\r\n        [_tableView beginUpdates];\r\n        [[self _patternSequence] removeChildrenAtIndexes:self.tableView.selectedRowIndexes];\r\n        [_tableView endUpdates];\r\n    }\r\n}\r\n<\/pre>\n<p>EDIT: I probably should only call super keyDown if it wasn&#8217;t the delete key\u2026otherwise we beep.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve seen many ways to implement delete in an NSTableView. Many are good. Many hardcode references to something they shouldn&#8217;t, and those are bad. Here&#8217;s an easy way: Subclass NSTableView and override keyDown: &#8211; (void)keyDown:(NSEvent&#8230; <a class=\"read-more\" href=\"https:\/\/www.corbinstreehouse.com\/blog\/2014\/04\/implementing-delete-in-an-nstableview\/\">[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":[6,86],"tags":[63,72],"class_list":["post-3631","post","type-post","status-publish","format-standard","hentry","category-cocoa","category-coding","tag-cocoa","tag-programming"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p8zn47-Wz","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/posts\/3631","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=3631"}],"version-history":[{"count":3,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/posts\/3631\/revisions"}],"predecessor-version":[{"id":5171,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/posts\/3631\/revisions\/5171"}],"wp:attachment":[{"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/media?parent=3631"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/categories?post=3631"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.corbinstreehouse.com\/blog\/wp-json\/wp\/v2\/tags?post=3631"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}