質問編集履歴

2

写真の追加

2017/06/11 16:30

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -8,6 +8,12 @@
8
8
 
9
9
  ![イメージ説明](5c3309b3edc131d5c2953dd091f118d4.png)
10
10
 
11
+ **エラー内容**
12
+
13
+ ![イメージ説明](485b7adfc3ccfa42aa26d26adcbe6986.png)
14
+
15
+ ![イメージ説明](eb061c4c1b46f12403a224c0427e8e89.png)
16
+
11
17
 
12
18
 
13
19
  **やったこと/試してみたこと**
@@ -62,7 +68,7 @@
62
68
 
63
69
  func tableView(_ tableVeiw: UITableView, numberOfRowsInSection section: Int) -> Int {
64
70
 
65
- return people.count+1
71
+ return people.count + 1
66
72
 
67
73
  }
68
74
 
@@ -70,31 +76,41 @@
70
76
 
71
77
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
72
78
 
73
-
79
+ if indexPath.row < people.count {
80
+
74
-
81
+ // 各personのセル
82
+
75
- let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
83
+ let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
76
-
77
-
78
-
84
+
85
+
86
+
79
- let person = people[indexPath.row]
87
+ let person = people[indexPath.row]
80
-
81
-
82
-
83
-
84
-
88
+
89
+
90
+
85
- cell!.textLabel!.text = person.value(forKey: "name") as? String
91
+ cell!.textLabel!.text = person.value(forKey: "name") as? String
86
-
87
- cell!.textLabel!.text = "add cell"
92
+
88
-
89
-
90
-
91
-
92
-
93
+
94
+
95
+
96
+
93
- return cell!
97
+ return cell!
98
+
94
-
99
+ } else {
100
+
95
-
101
+ cell!.textLabel.text = "add cell"
102
+
103
+
104
+
96
-
105
+ return cell
106
+
107
+ // FIXME:追加ボタンのセルを初期化してreturnする
108
+
97
- }
109
+ }
110
+
111
+ }
112
+
113
+
98
114
 
99
115
 
100
116
 

1

コードの修正

2017/06/11 16:29

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -20,22 +20,6 @@
20
20
 
21
21
  ```swift
22
22
 
23
- //
24
-
25
- // ViewController.swift
26
-
27
- // core data list EX
28
-
29
- //
30
-
31
- // Created by 西川継延 on 2016/12/24.
32
-
33
- // Copyright © 2016年 西川継延. All rights reserved.
34
-
35
- //
36
-
37
-
38
-
39
23
  import UIKit
40
24
 
41
25
  import CoreData
@@ -70,11 +54,15 @@
70
54
 
71
55
  }
72
56
 
57
+
58
+
59
+
60
+
73
61
 
74
62
 
75
63
  func tableView(_ tableVeiw: UITableView, numberOfRowsInSection section: Int) -> Int {
76
64
 
77
- return people.count
65
+ return people.count+1
78
66
 
79
67
  }
80
68
 
@@ -82,6 +70,8 @@
82
70
 
83
71
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
84
72
 
73
+
74
+
85
75
  let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
86
76
 
87
77
 
@@ -90,15 +80,23 @@
90
80
 
91
81
 
92
82
 
83
+
84
+
93
85
  cell!.textLabel!.text = person.value(forKey: "name") as? String
94
86
 
87
+ cell!.textLabel!.text = "add cell"
88
+
95
89
 
96
90
 
97
91
 
98
92
 
99
93
  return cell!
100
94
 
95
+
96
+
101
- }
97
+ }
98
+
99
+
102
100
 
103
101
 
104
102
 
@@ -110,7 +108,7 @@
110
108
 
111
109
  if editingStyle == .delete{
112
110
 
113
-
111
+ //self.tableView.setEditing(true, animated: true)
114
112
 
115
113
 
116
114
 
@@ -206,6 +204,74 @@
206
204
 
207
205
 
208
206
 
207
+
208
+
209
+ @IBAction func add cell(_ sender: Any) {
210
+
211
+ let alert = UIAlertController(title: "New name", message: "Enter a new name", preferredStyle: .alert)
212
+
213
+
214
+
215
+ let saveAction = UIAlertAction(title: "Save", style: .default) { (action) in
216
+
217
+ let textField = alert.textFields?.first
218
+
219
+ self.saveName(name: textField!.text!)
220
+
221
+ self.tableView.reloadData()
222
+
223
+ }
224
+
225
+
226
+
227
+ let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
228
+
229
+
230
+
231
+ alert.addTextField(configurationHandler: nil)
232
+
233
+ alert.addAction(saveAction)
234
+
235
+ alert.addAction(cancelAction)
236
+
237
+
238
+
239
+ present(alert, animated: true, completion: nil)
240
+
241
+ }
242
+
243
+
244
+
245
+ func save cell(name: String){
246
+
247
+ let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
248
+
249
+
250
+
251
+ let person = Person(entity: Person.entity(), insertInto: context)
252
+
253
+
254
+
255
+ person.setValue(name, forKey: "name")
256
+
257
+
258
+
259
+ do{
260
+
261
+ try context.save()
262
+
263
+ people.append(person)
264
+
265
+ } catch let error as NSError {
266
+
267
+ print("Could not save \(error), \(error.userInfo)")
268
+
269
+ }
270
+
271
+ }
272
+
273
+
274
+
209
275
  override func viewWillAppear(_ animated: Bool) {
210
276
 
211
277
  super.viewWillAppear(animated)
@@ -244,4 +310,6 @@
244
310
 
245
311
 
246
312
 
313
+
314
+
247
315
  ```