質問編集履歴

3

文章の修正

2017/09/09 13:57

投稿

misakiti
misakiti

スコア15

test CHANGED
File without changes
test CHANGED
@@ -136,6 +136,14 @@
136
136
 
137
137
  }
138
138
 
139
+
140
+
141
+ if let item = item as? Item {
142
+
143
+ return item.childrens.count
144
+
145
+ }
146
+
139
147
  return 0
140
148
 
141
149
  }

2

文章の追記

2017/09/09 13:57

投稿

misakiti
misakiti

スコア15

test CHANGED
File without changes
test CHANGED
@@ -30,6 +30,200 @@
30
30
 
31
31
 
32
32
 
33
+ 私が書いたコードとしては以下のようなものになります。
34
+
35
+ ``` swift3
36
+
37
+ import Cocoa
38
+
39
+
40
+
41
+ class ViewController: NSViewController, NSOutlineViewDataSource, NSOutlineViewDelegate {
42
+
43
+
44
+
45
+ let items: [Item] = {
46
+
47
+ return Item.itemsForNumberOf(parents: 5, childrens: 3)
48
+
49
+ }()
50
+
51
+
52
+
53
+ @IBOutlet var outlineView: NSOutlineView!
54
+
55
+
56
+
57
+ override func viewDidLoad() {
58
+
59
+ super.viewDidLoad()
60
+
61
+
62
+
63
+ // Do any additional setup after loading the view.
64
+
65
+ self.outlineView.delegate = self
66
+
67
+ self.outlineView.dataSource = self
68
+
69
+ }
70
+
71
+
72
+
73
+ override var representedObject: Any? {
74
+
75
+ didSet {
76
+
77
+ // Update the view, if already loaded.
78
+
79
+ }
80
+
81
+ }
82
+
83
+
84
+
85
+ func outlineView(_ outlineView: NSOutlineView, child index: Int, ofItem item: Any?) -> Any
86
+
87
+ {
88
+
89
+ if item == nil {
90
+
91
+ return items[index]
92
+
93
+ }
94
+
95
+
96
+
97
+ if let item = item as? Item, item.childrens.count > index {
98
+
99
+ return item.childrens[index]
100
+
101
+ }
102
+
103
+
104
+
105
+ return ""
106
+
107
+ }
108
+
109
+
110
+
111
+ func outlineView(_ outlineView: NSOutlineView, isItemExpandable item: Any) -> Bool
112
+
113
+ {
114
+
115
+ guard let item = item as? Item else {
116
+
117
+ return false
118
+
119
+ }
120
+
121
+
122
+
123
+ return !item.childrens.isEmpty
124
+
125
+ }
126
+
127
+
128
+
129
+ func outlineView(_ outlineView: NSOutlineView, numberOfChildrenOfItem item: Any?) -> Int
130
+
131
+ {
132
+
133
+ if item == nil {
134
+
135
+ return items.count
136
+
137
+ }
138
+
139
+ return 0
140
+
141
+ }
142
+
143
+
144
+
145
+ func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView?
146
+
147
+ {
148
+
149
+ guard let item = item as? Item else {
150
+
151
+ return nil
152
+
153
+ }
154
+
155
+
156
+
157
+ let v = outlineView.make(withIdentifier: "HeaderCell", owner: self) as? NSTableCellView
158
+
159
+
160
+
161
+ if let tf = v?.textField {
162
+
163
+ tf.stringValue = item.name
164
+
165
+ }
166
+
167
+ return v
168
+
169
+ }
170
+
171
+
172
+
173
+ struct Item {
174
+
175
+ let name: String
176
+
177
+ var childrens: [Item] = []
178
+
179
+
180
+
181
+ /// Create a helper function to recursivly create items
182
+
183
+ ///
184
+
185
+ /// - Parameters:
186
+
187
+ /// - parents: number of parent items
188
+
189
+ /// - childrens: number of children for each parent item
190
+
191
+ /// - Returns: array of Item
192
+
193
+ static func itemsForNumberOf(parents: Int, childrens: Int) -> [Item] {
194
+
195
+ var items: [Item] = []
196
+
197
+ for parentID in 1...parents {
198
+
199
+ var parent = Item(name: "Index \(parentID)",childrens: [])
200
+
201
+ for childrenID in 1...childrens {
202
+
203
+ let children = Item(name: "Index \(parentID).\(childrenID)",childrens: [])
204
+
205
+ parent.childrens.append(children)
206
+
207
+ }
208
+
209
+ items.append(parent)
210
+
211
+ }
212
+
213
+
214
+
215
+ return items
216
+
217
+ }
218
+
219
+ }
220
+
221
+ }
222
+
223
+ ```
224
+
225
+
226
+
33
227
  もし分かる方がいましたら教えていただけないでしょうか?
34
228
 
35
229
  よろしくお願い致します。

1

文字修正

2017/09/09 12:57

投稿

misakiti
misakiti

スコア15

test CHANGED
File without changes
test CHANGED
@@ -16,6 +16,8 @@
16
16
 
17
17
  子アイテムが表示されません。
18
18
 
19
+ (正しく動作するイメージとしてはindex 2項目の下に「index 2.1」、「index2.2」、「index2.3」と子項目を表示したいと考えて居ます。)
20
+
19
21
 
20
22
 
21
23
  ![イメージ説明](c4f81f6e04a18080a8fffaaf65244c4d.png)