質問編集履歴
2
画像を追加しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -301,3 +301,19 @@
|
|
301
301
|
Xcode 10.0
|
302
302
|
|
303
303
|
RealmSwift 3.11.1
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
###スクリーンショット
|
308
|
+
|
309
|
+
一番上のTextfieldとButton(ヘッダーに表示しているcellです)が、
|
310
|
+
|
311
|
+
![イメージ説明](f74d077a85fb13508ab068fe508a115e.png)
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
"d"を削除すると、一緒に表示されなくなってしまいます。
|
316
|
+
|
317
|
+
(削除するcellの位置に関係なく消えます)
|
318
|
+
|
319
|
+
![イメージ説明](14f75757c10dda2ef413cb92c3e316b5.png)
|
1
説明文、該当ソースコードの記載を修正しました。
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
【Swift】TableViewのcell削除で
|
1
|
+
【Swift】TableViewのcell削除でHeaderの要素が消えてしまう
|
test
CHANGED
@@ -1,20 +1,12 @@
|
|
1
|
-
いつもお世話になっております。
|
2
|
-
|
3
1
|
Swiftを勉強している初心者です。
|
4
2
|
|
5
3
|
|
6
4
|
|
7
|
-
UITableViewにRealmSwiftから取得したレコードを表示し、不要なレコードを削除する際にTableViewDelegateのed
|
5
|
+
UITableViewにRealmSwiftから取得したレコードを表示し、不要なレコードをスワイプで削除する際にしていますが、UITableViewDelegateのviewForHeaderInSectionで表示しているUITableViewCellまで一緒に消えてしまいます。
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
6
|
+
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
7
|
+
|
16
|
-
|
8
|
+
|
17
|
-
|
9
|
+
Headerのcellは削除されず、section内のレコードのみ削除されるようにしたいです。
|
18
10
|
|
19
11
|
|
20
12
|
|
@@ -26,9 +18,7 @@
|
|
26
18
|
|
27
19
|
### 該当のソースコード
|
28
20
|
|
29
|
-
```
|
30
|
-
|
31
|
-
|
21
|
+
```ViewController
|
32
22
|
|
33
23
|
|
34
24
|
|
@@ -40,7 +30,9 @@
|
|
40
30
|
|
41
31
|
|
42
32
|
|
43
|
-
class
|
33
|
+
class ViewController: UIViewController{
|
34
|
+
|
35
|
+
//抜粋
|
44
36
|
|
45
37
|
|
46
38
|
|
@@ -104,442 +96,202 @@
|
|
104
96
|
|
105
97
|
|
106
98
|
|
99
|
+
//TableView Delegate
|
100
|
+
|
101
|
+
extension ViewController: UITableViewDelegate{
|
102
|
+
|
103
|
+
//**********************
|
104
|
+
|
105
|
+
//ここでdelete時にHeaderのcellも一緒に消えてしまう
|
106
|
+
|
107
|
+
//**********************
|
108
|
+
|
109
|
+
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
|
110
|
+
|
111
|
+
if editingStyle == .delete{
|
112
|
+
|
113
|
+
try! realm.write {
|
114
|
+
|
115
|
+
if let wordEntities = wordEntities{
|
116
|
+
|
107
|
-
|
117
|
+
realm.delete(wordEntities[indexPath.row])
|
108
|
-
|
109
|
-
|
118
|
+
|
110
|
-
|
111
|
-
}
|
119
|
+
}
|
120
|
+
|
112
|
-
|
121
|
+
}
|
122
|
+
|
113
|
-
|
123
|
+
}
|
124
|
+
|
114
|
-
|
125
|
+
tableView.reloadData()
|
126
|
+
|
127
|
+
}
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
//display tableview header
|
132
|
+
|
115
|
-
func
|
133
|
+
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
|
116
|
-
|
117
|
-
|
134
|
+
|
118
|
-
|
119
|
-
|
135
|
+
let headerview = UIView()
|
136
|
+
|
137
|
+
headerview.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 100)
|
138
|
+
|
139
|
+
headerview.backgroundColor = UIColor.white
|
120
140
|
|
121
141
|
|
122
142
|
|
123
|
-
//No Category
|
124
|
-
|
125
|
-
if !category.isEmpty{
|
126
|
-
|
127
|
-
wordItem = wordEntities?.filter("wordId == %@", Id)
|
128
|
-
|
129
|
-
|
143
|
+
let searchbar = UISearchBar()
|
144
|
+
|
130
|
-
|
145
|
+
searchbar.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 50)
|
146
|
+
|
147
|
+
searchbar.placeholder = "検索"
|
148
|
+
|
131
|
-
|
149
|
+
headerview.addSubview(searchbar)
|
132
150
|
|
133
151
|
|
134
152
|
|
153
|
+
//**********************
|
154
|
+
|
135
|
-
|
155
|
+
//TableViewCellをHeaderに表示
|
136
|
-
|
137
|
-
|
156
|
+
|
138
|
-
|
139
|
-
|
157
|
+
//**********************
|
158
|
+
|
140
|
-
|
159
|
+
let cell = tableView.dequeueReusableCell(withIdentifier:"WordItemCell") as! WordTableViewCell
|
160
|
+
|
161
|
+
cell.frame = CGRect(x:0, y:55, width:self.view.frame.size.width, height:44)
|
162
|
+
|
163
|
+
cell.delegate = self
|
164
|
+
|
141
|
-
|
165
|
+
cell.dropdown.dataSource = arrayCategoryList()
|
142
|
-
|
166
|
+
|
143
|
-
|
167
|
+
headerview.addSubview(cell)
|
144
|
-
|
168
|
+
|
169
|
+
|
170
|
+
|
145
|
-
|
171
|
+
return headerview
|
146
|
-
|
147
|
-
|
172
|
+
|
148
|
-
|
149
|
-
|
173
|
+
}
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
//tableview header height size
|
178
|
+
|
179
|
+
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat{
|
180
|
+
|
181
|
+
return 100
|
182
|
+
|
183
|
+
}
|
184
|
+
|
185
|
+
}
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
//TableView DataSource
|
190
|
+
|
191
|
+
extension ViewController: UITableViewDataSource{
|
192
|
+
|
193
|
+
//display cell count
|
194
|
+
|
195
|
+
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
196
|
+
|
197
|
+
if let wordEntities = wordEntities{
|
198
|
+
|
199
|
+
return wordEntities.count
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
return 0
|
204
|
+
|
205
|
+
}
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
//display cell details
|
210
|
+
|
211
|
+
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
212
|
+
|
213
|
+
let cell = tableView.dequeueReusableCell(withIdentifier:"WordItemCell") as! WordTableViewCell
|
214
|
+
|
215
|
+
cell.delegate = self
|
216
|
+
|
217
|
+
cell.dropdown.dataSource = arrayCategoryList()
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
if let wordEntities = wordEntities{
|
222
|
+
|
223
|
+
cell.textfield.text = wordEntities[indexPath.row].word
|
224
|
+
|
225
|
+
cell.wordId = wordEntities[indexPath.row].wordId
|
226
|
+
|
227
|
+
cell.beforeWord = wordEntities[indexPath.row].word
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
if wordEntities[indexPath.row].categoryName != nil{
|
232
|
+
|
233
|
+
cell.categorybutton.setTitle(wordEntities[indexPath.row].categoryName, for: .normal)
|
234
|
+
|
235
|
+
cell.categoryName = wordEntities[indexPath.row].categoryName
|
236
|
+
|
237
|
+
cell.beforecategoryName = wordEntities[indexPath.row].categoryName
|
238
|
+
|
239
|
+
}
|
240
|
+
|
241
|
+
}
|
242
|
+
|
243
|
+
return cell
|
244
|
+
|
245
|
+
}
|
246
|
+
|
247
|
+
}
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
```
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
### 試したこと
|
258
|
+
|
259
|
+
delete後にreloadData()だけでなくreloadSections()も加えてみたら、削除1回目はCellも残ったままになりましたが、2回目以降削除すると結局消えてしまいます。
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
```ViewController
|
264
|
+
|
265
|
+
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
|
266
|
+
|
267
|
+
if editingStyle == .delete{
|
268
|
+
|
269
|
+
try! realm.write {
|
270
|
+
|
271
|
+
if let wordEntities = wordEntities{
|
272
|
+
|
273
|
+
realm.delete(wordEntities[indexPath.row])
|
274
|
+
|
275
|
+
}
|
276
|
+
|
277
|
+
}
|
278
|
+
|
279
|
+
}
|
280
|
+
|
281
|
+
//ここを追加
|
282
|
+
|
283
|
+
tableView.reloadSections(NSIndexSet(index: tableView.sectionIndexMinimumDisplayRowCount) as IndexSet, with: .none)
|
284
|
+
|
285
|
+
.none)
|
150
286
|
|
151
287
|
tableView.reloadData()
|
152
288
|
|
153
289
|
}
|
154
290
|
|
155
|
-
|
156
|
-
|
157
|
-
//insert new word
|
158
|
-
|
159
|
-
func insertWord(text: String, categoryName: String){
|
160
|
-
|
161
|
-
var categoryItem:Results<Category>? = nil
|
162
|
-
|
163
|
-
var category:Category? = nil
|
164
|
-
|
165
|
-
let item: [String: Any]
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
if !categoryName.isEmpty{
|
170
|
-
|
171
|
-
categoryItem = findCategoryItem(categoryName: categoryName)
|
172
|
-
|
173
|
-
category = Category(value: [
|
174
|
-
|
175
|
-
"categoryId": categoryItem!.first!.categoryId,
|
176
|
-
|
177
|
-
"categoryName": categoryItem!.first!.categoryName])
|
178
|
-
|
179
|
-
item = ["word": text,
|
180
|
-
|
181
|
-
"categoryId": categoryItem!.first!.categoryId,
|
182
|
-
|
183
|
-
"categoryName": categoryName]
|
184
|
-
|
185
|
-
}else{
|
186
|
-
|
187
|
-
item = ["word": text,
|
188
|
-
|
189
|
-
"categoryId": 0,
|
190
|
-
|
191
|
-
"categoryName": "No Category"]
|
192
|
-
|
193
|
-
}
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
let newWord = Words(value: item)
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
try! realm.write(){
|
202
|
-
|
203
|
-
realm.add(newWord)
|
204
|
-
|
205
|
-
if category != nil{
|
206
|
-
|
207
|
-
category?.words.append(newWord)
|
208
|
-
|
209
|
-
realm.create(Category.self, value: category!, update: true)
|
210
|
-
|
211
|
-
}
|
212
|
-
|
213
|
-
}
|
214
|
-
|
215
|
-
}
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
//update word
|
220
|
-
|
221
|
-
func updateWord(text: String, categoryName: String, wordItem: Words){
|
222
|
-
|
223
|
-
var categoryItem:Results<Category>? = nil
|
224
|
-
|
225
|
-
var category:Category? = nil
|
226
|
-
|
227
|
-
let item: [String: Any]
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
categoryItem = findCategoryItem(categoryName: categoryName)
|
232
|
-
|
233
|
-
if categoryItem?.count != 0{
|
234
|
-
|
235
|
-
category = Category(value: [
|
236
|
-
|
237
|
-
"categoryId": categoryItem!.first!.categoryId,
|
238
|
-
|
239
|
-
"categoryName": categoryItem!.first!.categoryName])
|
240
|
-
|
241
|
-
item = ["wordId": wordItem.wordId!,
|
242
|
-
|
243
|
-
"word": text,
|
244
|
-
|
245
|
-
"updateDate": Date(),
|
246
|
-
|
247
|
-
"categoryId": categoryItem!.first!.categoryId,
|
248
|
-
|
249
|
-
"categoryName": categoryItem!.first!.categoryName]
|
250
|
-
|
251
|
-
}else{
|
252
|
-
|
253
|
-
item = ["wordId": wordItem.wordId!,
|
254
|
-
|
255
|
-
"word": text,
|
256
|
-
|
257
|
-
// "userId": "test-user",
|
258
|
-
|
259
|
-
"categoryName": "No Category",
|
260
|
-
|
261
|
-
"updateDate": Date()]
|
262
|
-
|
263
|
-
}
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
var oldCategoryItem:Results<Category>? = nil
|
268
|
-
|
269
|
-
var removeWordItem:Category? = nil
|
270
|
-
|
271
|
-
var removeWordItemIndex:Int? = nil
|
272
|
-
|
273
|
-
var oldCategory:Category? = nil
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
if wordItem.categoryId != 0{
|
278
|
-
|
279
|
-
oldCategoryItem = findCategoryItem(categoryName: wordItem.categoryName!)
|
280
|
-
|
281
|
-
removeWordItem = Array(oldCategoryItem!).first
|
282
|
-
|
283
|
-
removeWordItemIndex = removeWordItem!.words.index(matching: "wordId == %@", wordItem.wordId!)
|
284
|
-
|
285
|
-
oldCategory = Category(value: removeWordItem!)
|
286
|
-
|
287
|
-
}
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
let editWord = Words(value: item)
|
294
|
-
|
295
|
-
try! realm.write(){
|
296
|
-
|
297
|
-
realm.add(editWord, update: true)
|
298
|
-
|
299
|
-
if category != nil{
|
300
|
-
|
301
|
-
category!.words.append(editWord)
|
302
|
-
|
303
|
-
realm.create(Category.self, value: category!, update: true)
|
304
|
-
|
305
|
-
}
|
306
|
-
|
307
|
-
if removeWordItemIndex != nil{
|
308
|
-
|
309
|
-
oldCategory!.words.remove(at: removeWordItemIndex!)
|
310
|
-
|
311
|
-
realm.create(Category.self, value: oldCategory!, update: true)
|
312
|
-
|
313
|
-
}
|
314
|
-
|
315
|
-
}
|
316
|
-
|
317
|
-
}
|
318
|
-
|
319
|
-
}
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
//TableView Delegate
|
324
|
-
|
325
|
-
extension WordsListViewController: UITableViewDelegate{
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
//**********************
|
330
|
-
|
331
|
-
//ここでdelete時にHeaderのcellだけ消えてしまう
|
332
|
-
|
333
|
-
//**********************
|
334
|
-
|
335
|
-
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
|
336
|
-
|
337
|
-
if editingStyle == .delete{
|
338
|
-
|
339
|
-
try! realm.write {
|
340
|
-
|
341
|
-
if let wordEntities = wordEntities{
|
342
|
-
|
343
|
-
realm.delete(wordEntities[indexPath.row])
|
344
|
-
|
345
|
-
}
|
346
|
-
|
347
|
-
}
|
348
|
-
|
349
|
-
}
|
350
|
-
|
351
|
-
tableView.reloadData()
|
352
|
-
|
353
|
-
}
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
//display tableview header
|
358
|
-
|
359
|
-
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
|
360
|
-
|
361
|
-
let headerview = UIView()
|
362
|
-
|
363
|
-
headerview.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 100)
|
364
|
-
|
365
|
-
headerview.backgroundColor = UIColor.white
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
//まだ張りぼて状態...
|
370
|
-
|
371
|
-
let searchbar = UISearchBar()
|
372
|
-
|
373
|
-
searchbar.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 50)
|
374
|
-
|
375
|
-
searchbar.placeholder = "検索"
|
376
|
-
|
377
|
-
headerview.addSubview(searchbar)
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
//**********************
|
382
|
-
|
383
|
-
//該当のTableViewCellの生成箇所
|
384
|
-
|
385
|
-
//**********************
|
386
|
-
|
387
|
-
let cell = tableView.dequeueReusableCell(withIdentifier:"WordItemCell") as! WordTableViewCell
|
388
|
-
|
389
|
-
cell.frame = CGRect(x:0, y:55, width:self.view.frame.size.width, height:44)
|
390
|
-
|
391
|
-
cell.delegate = self
|
392
|
-
|
393
|
-
cell.dropdown.dataSource = arrayCategoryList()
|
394
|
-
|
395
|
-
headerview.addSubview(cell)
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
return headerview
|
400
|
-
|
401
|
-
}
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
//tableview header height size
|
406
|
-
|
407
|
-
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat{
|
408
|
-
|
409
|
-
return 100
|
410
|
-
|
411
|
-
}
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
}
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
//TableView DataSource
|
420
|
-
|
421
|
-
extension WordsListViewController: UITableViewDataSource{
|
422
|
-
|
423
|
-
//display cell count
|
424
|
-
|
425
|
-
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
426
|
-
|
427
|
-
if let wordEntities = wordEntities{
|
428
|
-
|
429
|
-
return wordEntities.count
|
430
|
-
|
431
|
-
}
|
432
|
-
|
433
|
-
return 0
|
434
|
-
|
435
|
-
}
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
//display cell details
|
440
|
-
|
441
|
-
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
442
|
-
|
443
|
-
let cell = tableView.dequeueReusableCell(withIdentifier:"WordItemCell") as! WordTableViewCell
|
444
|
-
|
445
|
-
cell.delegate = self
|
446
|
-
|
447
|
-
cell.dropdown.dataSource = arrayCategoryList()
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
if let wordEntities = wordEntities{
|
452
|
-
|
453
|
-
cell.textfield.text = wordEntities[indexPath.row].word
|
454
|
-
|
455
|
-
cell.wordId = wordEntities[indexPath.row].wordId
|
456
|
-
|
457
|
-
cell.beforeWord = wordEntities[indexPath.row].word
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
if wordEntities[indexPath.row].categoryName != nil{
|
462
|
-
|
463
|
-
cell.categorybutton.setTitle(wordEntities[indexPath.row].categoryName, for: .normal)
|
464
|
-
|
465
|
-
cell.categoryName = wordEntities[indexPath.row].categoryName
|
466
|
-
|
467
|
-
cell.beforecategoryName = wordEntities[indexPath.row].categoryName
|
468
|
-
|
469
|
-
}
|
470
|
-
|
471
|
-
}
|
472
|
-
|
473
|
-
return cell
|
474
|
-
|
475
|
-
}
|
476
|
-
|
477
|
-
}
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
//Textfield Delegate(extension WordTableViewCell)
|
482
|
-
|
483
|
-
extension WordsListViewController: InputTextTableCellDelegate{
|
484
|
-
|
485
|
-
//textfield has finished to edit
|
486
|
-
|
487
|
-
func textFieldDidEndEditing(cell: WordTableViewCell, value: String) -> () {
|
488
|
-
|
489
|
-
if value != cell.beforeWord || cell.categoryName != cell.beforecategoryName {
|
490
|
-
|
491
|
-
saveWord(Id: cell.wordId!, text: value, category: cell.categoryName!)
|
492
|
-
|
493
|
-
}
|
494
|
-
|
495
|
-
}
|
496
|
-
|
497
|
-
}
|
498
|
-
|
499
|
-
|
500
|
-
|
501
291
|
```
|
502
292
|
|
503
293
|
|
504
294
|
|
505
|
-
### 試したこと
|
506
|
-
|
507
|
-
delete後にreloadData()だけでなくreloadSections()も加えてみたら、削除1回目はCellも残った状態になりますが、2回目以降もdeleteすると表示されなくなってしまいます。
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
```
|
512
|
-
|
513
|
-
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
|
514
|
-
|
515
|
-
if editingStyle == .delete{
|
516
|
-
|
517
|
-
try! realm.write {
|
518
|
-
|
519
|
-
if let wordEntities = wordEntities{
|
520
|
-
|
521
|
-
realm.delete(wordEntities[indexPath.row])
|
522
|
-
|
523
|
-
}
|
524
|
-
|
525
|
-
}
|
526
|
-
|
527
|
-
}
|
528
|
-
|
529
|
-
//↓ここを追加
|
530
|
-
|
531
|
-
tableView.reloadSections(NSIndexSet(index: tableView.sectionIndexMinimumDisplayRowCount) as IndexSet, with: .none)
|
532
|
-
|
533
|
-
.none)
|
534
|
-
|
535
|
-
tableView.reloadData()
|
536
|
-
|
537
|
-
}
|
538
|
-
|
539
|
-
```
|
540
|
-
|
541
|
-
|
542
|
-
|
543
295
|
|
544
296
|
|
545
297
|
### 補足情報(FW/ツールのバージョンなど)
|