{"id":1479,"date":"2022-03-23T22:46:32","date_gmt":"2022-03-23T13:46:32","guid":{"rendered":"https:\/\/www.typea.info\/blog\/?p=1479"},"modified":"2022-03-23T22:49:46","modified_gmt":"2022-03-23T13:49:46","slug":"nstableview_viewbased_macos","status":"publish","type":"post","link":"https:\/\/www.typea.info\/blog\/index.php\/2022\/03\/23\/nstableview_viewbased_macos\/","title":{"rendered":"NSTableView ViewBased\u3092\u8a66\u3057\u3066\u307f\u308b(macos)"},"content":{"rendered":"<p><a href=\"https:\/\/www.typea.info\/blog\/index.php\/2022\/03\/11\/nstableview_celbased\/\">NSTableView CellBased\u3092\u8a66\u3057\u3066\u307f\u305f<\/a>\u306e\u3060\u304c\u3001ViewBased\u3068\u3044\u3046\u5b9f\u88c5\u65b9\u6cd5\u3082\u3042\u308b\u3089\u3057\u3044\u306e\u3067\u3001\u8a66\u3057\u3066\u307f\u305f\u3002<\/p>\n<p>\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3092\u9078\u629e\u3057\u305f\u3089\u3001\u914d\u4e0b\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u8868\u793a\u3055\u305b\u308b\u3002<\/p>\n<p><a href=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2022\/03\/tableview_viewbased.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" title=\"tableview_viewbased.gif\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2022\/03\/tableview_viewbased.gif\" alt=\"tableview_viewbased\" width=\"360\" height=\"240\" border=\"0\" \/><\/a><\/p>\n<p>\u306e\u3060\u304c\u3001\u306a\u3093\u306e\u305f\u3081\u306b\u4f7f\u3044\u5206\u3051\u308b\u306e\u304b\u3088\u304f\u308f\u304b\u3089\u306a\u3044\u3002\u3002\u3002<\/p>\n<p>\u95a2\u9023\u30af\u30e9\u30b9\u306e\u30de\u30cb\u30e5\u30a2\u30eb\u3092\u4e00\u90e8\u629c\u7c8b<\/p>\n<p><a href=\"https:\/\/developer.apple.com\/documentation\/appkit\/nstableview\">NSTableView<\/a><\/p>\n<ul>\n<li>NSTableView\u306e\u30b5\u30d6\u30af\u30e9\u30b9\u5316\u306f\u901a\u5e38\u5fc5\u8981\u3042\u308a\u307e\u305b\u3093\u3002<\/li>\n<li>\u305d\u306e\u4ee3\u308f\u308a\u306b\u3001NSTableViewDelegate \u30d7\u30ed\u30c8\u30b3\u30eb\u306b\u6e96\u62e0\u3057\u305f\u30c7\u30ea\u30b2\u30fc\u30c8\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304a\u3088\u3073\u3001NSTableViewDataSource \u30d7\u30ed\u30c8\u30b3\u30eb\u306b\u6e96\u62e0\u3057\u305f\u30c7\u30fc\u30bf\u30bd\u30fc\u30b9\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u4f7f\u7528\u3057\u3066\u30c6\u30fc\u30d6\u30eb\u30d3\u30e5\u30fc\u3092\u30ab\u30b9\u30bf\u30de\u30a4\u30ba\u3057\u307e\u3059\u3002<\/li>\n<li>\u307e\u305f\u306f\u3001\u4ee5\u4e0b\u306e\u30b5\u30d6\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u306e\u3044\u305a\u308c\u304b\u3092\u30b5\u30d6\u30af\u30e9\u30b9\u5316\u3059\u308b\u3053\u3068\u3067\u3001\u5b9f\u73fe\u3067\u304d\u307e\u3059\u3002\n<ul>\n<li>\u30bb\u30eb\uff08<a href=\"https:\/\/developer.apple.com\/documentation\/appkit\/nscell\">NSCell<\/a>\u30d9\u30fc\u30b9\u306e\u30c6\u30fc\u30d6\u30eb\u30d3\u30e5\u30fc\u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\uff09<\/li>\n<li>\u884c\u30bb\u30eb\u30d3\u30e5\u30fc\u307e\u305f\u306f\u884c\u30d3\u30e5\u30fc\uff08<a href=\"https:\/\/developer.apple.com\/documentation\/appkit\/nsview\">NSView<\/a>\u30d9\u30fc\u30b9\u306e\u30c6\u30fc\u30d6\u30eb\u30d3\u30e5\u30fc\u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\uff09<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><a href=\"https:\/\/developer.apple.com\/documentation\/appkit\/nstableviewdelegate\">NSTableViewDelegate<\/a><\/p>\n<ul>\n<li>TableView\u306e\u632f\u308b\u821e\u3044\u3092\u30ab\u30b9\u30bf\u30de\u30a4\u30ba\u3059\u308b\u305f\u3081\u306b\u5b9f\u88c5\u3059\u308b\u30aa\u30d7\u30b7\u30e7\u30f3\u30e1\u30bd\u30c3\u30c9\u306e\u30bb\u30c3\u30c8<\/li>\n<li>TableView\u306e\u30b5\u30d6\u30af\u30e9\u30b9\u3092\u4f5c\u6210\u305b\u305a\u306b\u52d5\u4f5c\u3092\u30ab\u30b9\u30bf\u30de\u30a4\u30ba\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u30c6\u30fc\u30d6\u30eb\u306e\u884c\u3068\u5217\u306e\u30d3\u30e5\u30fc\u3092\u63d0\u4f9b\u3057\u3001\u5217\u306e\u4e26\u3079\u66ff\u3048\u3084\u30b5\u30a4\u30ba\u5909\u66f4\u3001\u884c\u306e\u9078\u629e\u306a\u3069\u306e\u6a5f\u80fd\u3092\u30b5\u30dd\u30fc\u30c8<\/li>\n<\/ul>\n<p><a href=\"https:\/\/developer.apple.com\/documentation\/appkit\/nscell\">NSCell<\/a><\/p>\n<ul>\n<li>NSView\u306e\u30b5\u30d6\u30af\u30e9\u30b9\u306e\u30aa\u30fc\u30d0\u30fc\u30d8\u30c3\u30c9\u306a\u3057\u3067\u3001\u30d3\u30e5\u30fc\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u30c6\u30ad\u30b9\u30c8\u3084\u753b\u50cf\u3092\u8868\u793a\u3059\u308b\u305f\u3081\u306e\u30e1\u30ab\u30cb\u30ba\u30e0<\/li>\n<li>\u30bb\u30eb\u306f\u3001NSControl\u30af\u30e9\u30b9\u306e\u307b\u3068\u3093\u3069\u3067\u3001\u305d\u306e\u5185\u90e8\u52d5\u4f5c\u3092\u5b9f\u88c5\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u3066\u3044\u308b<\/li>\n<\/ul>\n<p><a href=\"https:\/\/developer.apple.com\/documentation\/appkit\/nsview\">NSView<\/a><\/p>\n<ul>\n<li>\u30a2\u30d7\u30ea\u5185\u3067\u63cf\u753b\u3001\u5370\u5237\u3001\u30a4\u30d9\u30f3\u30c8\u51e6\u7406\u306a\u3069\u3092\u884c\u3046\u305f\u3081\u306e\u57fa\u76e4<\/li>\n<li>\u901a\u5e38\u3001NSView\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u76f4\u63a5\u4f7f\u7528\u3059\u308b\u3053\u3068\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u4ee3\u308f\u308a\u306b\u3001NSView\u304b\u3089\u6d3e\u751f\u3057\u305f\u30af\u30e9\u30b9\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u4f7f\u7528\u3059\u308b\u304b\u3001NSView\u3092\u81ea\u5206\u3067\u30b5\u30d6\u30af\u30e9\u30b9\u5316\u3057\u3001\u5fc5\u8981\u306a\u52d5\u4f5c\u3092\u5b9f\u88c5\u3059\u308b\u305f\u3081\u306b\u305d\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u30aa\u30fc\u30d0\u30fc\u30e9\u30a4\u30c9\u3057\u307e\u3059\u3002NSView\u30af\u30e9\u30b9\uff08\u307e\u305f\u306f\u305d\u306e\u30b5\u30d6\u30af\u30e9\u30b9\uff09\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306f\u3001\u4e00\u822c\u7684\u306b\u30d3\u30e5\u30fc\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068\u3057\u3066\u3001\u307e\u305f\u306f\u5358\u306b\u30d3\u30e5\u30fc\u3068\u3057\u3066\u77e5\u3089\u308c\u3066\u3044\u307e\u3059<\/li>\n<\/ul>\n<p>NSTableColumn \u3092\u9078\u629e\u3057\u00a0idenfifer \u3092\u6307\u5b9a\u00a0tableView \u30e1\u30bd\u30c3\u30c9\u304c\u547c\u3073\u51fa\u3055\u308c\u308b\u3068\u304d\u306b\u3001identifer \u3092\u30ad\u30fc\u306b\u5024\u3092\u53d6\u5f97\u3055\u305b\u308b\u3002<\/p>\n<p><a href=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2022\/03\/nstableview_viewbased00003.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" title=\"nstableview_viewbased00003.png\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2022\/03\/nstableview_viewbased00003.png\" alt=\"Nstableview viewbased00003\" width=\"786\" height=\"359\" border=\"0\" \/><\/a><\/p>\n<p>ViewBased\u3068\u304b\u3001CellBased\u3068\u3044\u3046\u5207\u308a\u53e3\u3067\u5207\u308a\u5206\u3051\u308b\u306e\u306f\u3061\u3087\u3063\u3068\u9055\u3046\u611f\u304c\u3059\u308b\u306a\u3002<\/p>\n<p>\u3068\u308a\u3042\u3048\u305a\u3001\u30e1\u30e2\u3057\u3066\u304a\u304f\u3002<\/p>\n<pre class=\"prettyprint\">import Cocoa\nimport Foundation\n\nclass ViewController: NSViewController, NSTableViewDelegate, NSTableViewDataSource {\n    private static let fm = FileManager.default\n    private var files: [File] = []\n    \n    @IBOutlet weak var fileTable: NSTableView!\n    @IBOutlet weak var directoryPath: NSPathControl!\n    \n    override func viewDidLoad() {\n        super.viewDidLoad()\n\n        \/\/ Do any additional setup after loading the view.\n    }\n    \n    func numberOfRows(in tableView: NSTableView) -&gt; Int {\n        return files.count;\n    }\n\n    func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -&gt; NSView? {\n       \n        let file = files[row]\n        let cell = tableView.makeView(withIdentifier: (tableColumn!.identifier), owner: self) as? NSTableCellView\n        let celval = file.get(key: tableColumn!.identifier.rawValue)\n        cell?.textField?.stringValue = celval!\n\n        return cell\n    }\n    \n    @IBAction func chooseDir(_ sender: Any) {\n        let dialog = NSOpenPanel();\n\n        dialog.title                   = \"Choose Directory\"\n        dialog.showsResizeIndicator    = true\n        dialog.showsHiddenFiles        = false\n        dialog.allowsMultipleSelection = false\n        dialog.canChooseDirectories    = true\n        dialog.canChooseFiles          = false\n\n        if (dialog.runModal() ==  NSApplication.ModalResponse.OK) {\n            let result = dialog.url\n\n            if (result != nil) {\n                let path: String = result!.path\n                directoryPath.url = URL(fileURLWithPath: path)\n                \n                self.files.removeAll()\n                \n                loadFiles(basePath: path)\n                \n                self.fileTable.reloadData()\n            }\n        }\n    }\n    private func loadFiles(basePath: String) {\n        if !isDirectory(path: basePath) {\n            return\n        }\n        files.append(File(path: basePath, name: basePath, isDirectory: true, isOn: false))\n        \n        do {\n            let fileNames = try ViewController.fm.contentsOfDirectory(atPath: basePath)\n            \n            for fileName in fileNames {\n                let childPath = basePath + \"\/\" + fileName\n                if isDirectory(path: childPath) {\n                    loadFiles(basePath:childPath)\n                }\n                print(\"\\t\" + childPath)\n                files.append(File(path: childPath, name: fileName, isDirectory: false, isOn: true))\n            }\n            \n            \n        } catch {\n            print(error)\n        }\n    }\n    \n    private func isDirectory(path: String) -&gt; Bool {\n        var isDir = ObjCBool(false)\n        ViewController.fm.fileExists(atPath: path, isDirectory: &amp;isDir)\n        return isDir.boolValue\n    }\n    \n    override var representedObject: Any? {\n        didSet {\n        \/\/ Update the view, if already loaded.\n        }\n    }\n}\n\npublic class File {\n    let path: String\n    let name: String\n    let isDirectory: Bool\n    var isOn: Bool = false\n    \n    init(path: String, name: String, isDirectory: Bool, isOn: Bool) {\n        self.path = path\n        self.name = name\n        self.isDirectory = isDirectory\n        self.isOn = isOn\n    }\n    public func get(key: String) -&gt; String? {\n        switch key {\n        case \"path\":\n            return self.path\n        case \"name\":\n            return self.name\n        default:\n            return nil\n        }\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>NSTableView CellBased\u3092\u8a66\u3057\u3066\u307f\u305f\u306e\u3060\u304c\u3001ViewBased\u3068\u3044\u3046\u5b9f\u88c5\u65b9\u6cd5\u3082\u3042\u308b\u3089\u3057\u3044\u306e\u3067\u3001\u8a66\u3057\u3066\u307f\u305f\u3002 \u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3092\u9078\u629e\u3057\u305f\u3089\u3001\u914d\u4e0b\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u8868\u793a\u3055\u305b\u308b\u3002 \u306e\u3060\u304c\u3001\u306a\u3093\u306e\u305f\u3081\u306b\u4f7f\u3044\u5206\u3051\u308b\u306e\u304b\u3088\u304f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1478,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"sns_share_botton_hide":"","vkExUnit_sns_title":"","_vk_print_noindex":"","sitemap_hide":"","_veu_custom_css":"","veu_display_promotion_alert":"","vkexunit_cta_each_option":"","footnotes":""},"categories":[170,127],"tags":[],"class_list":["post-1479","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cocoa","category-swift"],"veu_head_title_object":{"title":"","add_site_title":""},"_links":{"self":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1479","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=1479"}],"version-history":[{"count":2,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1479\/revisions"}],"predecessor-version":[{"id":1481,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1479\/revisions\/1481"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/media\/1478"}],"wp:attachment":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=1479"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=1479"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=1479"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}