質問編集履歴

3

コードの修正

2017/05/30 22:59

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -30,266 +30,208 @@
30
30
 
31
31
  var people = [Person]()
32
32
 
33
+ var kei = ["追加ボタン"]
34
+
35
+
36
+
37
+ override func viewDidLoad() {
38
+
39
+ super.viewDidLoad()
40
+
41
+
42
+
43
+
44
+
45
+ title = "\"The List\""
46
+
47
+ tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
48
+
49
+ }
50
+
51
+
52
+
53
+ func tableView(_ tableVeiw: UITableView, numberOfRowsInSection section: Int) -> Int {
54
+
55
+ return people.count
56
+
57
+ //return kei.count
58
+
59
+ }
60
+
61
+
62
+
63
+ func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
64
+
65
+ let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
66
+
67
+
68
+
69
+ let person = people[indexPath.row]
70
+
71
+
72
+
73
+ cell!.textLabel!.text = person.value(forKey: "name") as? String
74
+
75
+ //cell!.textLabel!.text = kei.value(forkey: "name") as? String
76
+
77
+
78
+
79
+ return cell!
80
+
81
+ }
82
+
83
+
84
+
85
+ func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
86
+
87
+ let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
88
+
89
+
90
+
91
+ if editingStyle == .delete{
92
+
93
+ //self.tableView.setEditing(true, animated: true)
94
+
95
+
96
+
97
+ context.delete(people[indexPath.row])
98
+
99
+
100
+
101
+ (UIApplication.shared.delegate as! AppDelegate).saveContext()
102
+
103
+
104
+
105
+ do {
106
+
107
+ people = try context.fetch(Person.fetchRequest())
108
+
109
+ }catch let error as NSError {
110
+
111
+ print("Could not save \(error), \(error.userInfo)")
112
+
113
+ }
114
+
115
+ tableView.reloadData()
116
+
117
+ }
118
+
119
+ }
120
+
121
+
122
+
123
+ @IBAction func addName(_ sender: Any) {
124
+
125
+ let alert = UIAlertController(title: "New name", message: "Enter a new name", preferredStyle: .alert)
126
+
127
+
128
+
129
+ let saveAction = UIAlertAction(title: "Save", style: .default) { (action) in
130
+
131
+ let textField = alert.textFields?.first
132
+
133
+ self.saveName(name: textField!.text!)
134
+
135
+ self.tableView.reloadData()
136
+
137
+ }
138
+
139
+
140
+
141
+ let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
142
+
143
+
144
+
145
+ alert.addTextField(configurationHandler: nil)
146
+
147
+ alert.addAction(saveAction)
148
+
149
+ alert.addAction(cancelAction)
150
+
151
+
152
+
153
+ present(alert, animated: true, completion: nil)
154
+
155
+ }
156
+
157
+
158
+
159
+ func saveName(name: String){
160
+
161
+ let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
162
+
163
+
164
+
165
+ let person = Person(entity: Person.entity(), insertInto: context)
166
+
167
+
168
+
169
+ person.setValue(name, forKey: "name")
170
+
171
+
172
+
173
+ do{
174
+
175
+ try context.save()
176
+
177
+ people.append(person)
178
+
179
+ } catch let error as NSError {
180
+
181
+ print("Could not save \(error), \(error.userInfo)")
182
+
183
+ }
184
+
185
+ }
186
+
187
+
188
+
189
+ override func viewWillAppear(_ animated: Bool) {
190
+
191
+ super.viewWillAppear(animated)
192
+
193
+
194
+
195
+ let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
196
+
197
+
198
+
199
+ do{
200
+
201
+ let result = try context.fetch(Person.fetchRequest())
202
+
203
+ people = result as! [Person]
204
+
205
+ }catch let error as NSError {
206
+
207
+ print("Could not save \(error), \(error.userInfo)")
208
+
209
+ }
210
+
211
+ }
212
+
213
+
214
+
215
+ override func didReceiveMemoryWarning() {
216
+
217
+ super.didReceiveMemoryWarning()
218
+
219
+ }
220
+
221
+
222
+
223
+ }
224
+
225
+
226
+
227
+
228
+
229
+ ```
230
+
231
+ var people = [Person]()
232
+
33
233
  var kei = ["cell 追加"]
34
234
 
35
-
36
-
37
- override func viewDidLoad() {
38
-
39
- super.viewDidLoad()
40
-
41
-
42
-
43
-
44
-
45
- title = "\"The List\""
46
-
47
- tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
48
-
49
- }
50
-
51
-
52
-
53
- func tableView(_ tableVeiw: UITableView, numberOfRowsInSection section: Int) -> Int {
235
+ ここの部分をひとまとめにしたらアラートで記入した部分のcellと他に「追加ボタン」と固定したしたcellが追加されるとされるのではないかと思うのですがどうすればいいか分からなくて困っています
54
-
55
- return people.count
236
+
56
-
57
- //return kei.count
237
+ 何かアドバイスかコードでもいいので教えていだきたいです。
58
-
59
- }
60
-
61
-
62
-
63
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
64
-
65
- let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
66
-
67
-
68
-
69
- let person = people[indexPath.row]
70
-
71
-
72
-
73
- cell!.textLabel!.text = person.value(forKey: "name") as? String
74
-
75
- //cell!.textLabel!.text = kei.value(forkey: "name") as? String
76
-
77
-
78
-
79
- return cell!
80
-
81
- }
82
-
83
-
84
-
85
- func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
86
-
87
- let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
88
-
89
-
90
-
91
- if editingStyle == .delete{
92
-
93
- //self.tableView.setEditing(true, animated: true)
94
-
95
-
96
-
97
- context.delete(people[indexPath.row])
98
-
99
-
100
-
101
- (UIApplication.shared.delegate as! AppDelegate).saveContext()
102
-
103
-
104
-
105
- do {
106
-
107
- people = try context.fetch(Person.fetchRequest())
108
-
109
- }catch let error as NSError {
110
-
111
- print("Could not save \(error), \(error.userInfo)")
112
-
113
- }
114
-
115
- tableView.reloadData()
116
-
117
- }
118
-
119
- }
120
-
121
-
122
-
123
- @IBAction func addName(_ sender: Any) {
124
-
125
- let alert = UIAlertController(title: "New name", message: "Enter a new name", preferredStyle: .alert)
126
-
127
-
128
-
129
- let saveAction = UIAlertAction(title: "Save", style: .default) { (action) in
130
-
131
- let textField = alert.textFields?.first
132
-
133
- self.saveName(name: textField!.text!)
134
-
135
- self.tableView.reloadData()
136
-
137
- }
138
-
139
-
140
-
141
- let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
142
-
143
-
144
-
145
- alert.addTextField(configurationHandler: nil)
146
-
147
- alert.addAction(saveAction)
148
-
149
- alert.addAction(cancelAction)
150
-
151
-
152
-
153
- present(alert, animated: true, completion: nil)
154
-
155
- }
156
-
157
-
158
-
159
- func saveName(name: String){
160
-
161
- let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
162
-
163
-
164
-
165
- let person = Person(entity: Person.entity(), insertInto: context)
166
-
167
-
168
-
169
- person.setValue(name, forKey: "name")
170
-
171
-
172
-
173
- do{
174
-
175
- try context.save()
176
-
177
- people.append(person)
178
-
179
- } catch let error as NSError {
180
-
181
- print("Could not save \(error), \(error.userInfo)")
182
-
183
- }
184
-
185
- }
186
-
187
-
188
-
189
- /*@IBAction func kei(_ sender: Any) {
190
-
191
- let alert = UIAlertController(title: "New name", message: "Enter a new name", preferredStyle: .alert)
192
-
193
-
194
-
195
- let saveAction = UIAlertAction(title: "Save", style: .default) { (action) in
196
-
197
- let textField = alert.textFields?.first
198
-
199
- self.saveName(name: textField!.text!)
200
-
201
- self.tableView.reloadData()
202
-
203
- }
204
-
205
-
206
-
207
- let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
208
-
209
-
210
-
211
- alert.addTextField(configurationHandler: nil)
212
-
213
- alert.addAction(saveAction)
214
-
215
- alert.addAction(cancelAction)
216
-
217
-
218
-
219
- present(alert, animated: true, completion: nil)
220
-
221
- }
222
-
223
-
224
-
225
- func saveName(name: String){
226
-
227
- let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
228
-
229
-
230
-
231
- let person = Person(entity: Person.entity(), insertInto: context)
232
-
233
-
234
-
235
- person.setValue(name, forKey: "name")
236
-
237
-
238
-
239
- do{
240
-
241
- try context.save()
242
-
243
- people.append(person)
244
-
245
- } catch let error as NSError {
246
-
247
- print("Could not save \(error), \(error.userInfo)")
248
-
249
- }
250
-
251
- }
252
-
253
- */
254
-
255
- override func viewWillAppear(_ animated: Bool) {
256
-
257
- super.viewWillAppear(animated)
258
-
259
-
260
-
261
- let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
262
-
263
-
264
-
265
- do{
266
-
267
- let result = try context.fetch(Person.fetchRequest())
268
-
269
- people = result as! [Person]
270
-
271
- }catch let error as NSError {
272
-
273
- print("Could not save \(error), \(error.userInfo)")
274
-
275
- }
276
-
277
- }
278
-
279
-
280
-
281
- override func didReceiveMemoryWarning() {
282
-
283
- super.didReceiveMemoryWarning()
284
-
285
- }
286
-
287
-
288
-
289
- }
290
-
291
-
292
-
293
-
294
-
295
- ```

2

文法の編集

2017/05/30 22:59

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -30,6 +30,8 @@
30
30
 
31
31
  var people = [Person]()
32
32
 
33
+ var kei = ["cell 追加"]
34
+
33
35
 
34
36
 
35
37
  override func viewDidLoad() {
@@ -52,6 +54,8 @@
52
54
 
53
55
  return people.count
54
56
 
57
+ //return kei.count
58
+
55
59
  }
56
60
 
57
61
 
@@ -68,6 +72,8 @@
68
72
 
69
73
  cell!.textLabel!.text = person.value(forKey: "name") as? String
70
74
 
75
+ //cell!.textLabel!.text = kei.value(forkey: "name") as? String
76
+
71
77
 
72
78
 
73
79
  return cell!
@@ -180,11 +186,77 @@
180
186
 
181
187
 
182
188
 
189
+ /*@IBAction func kei(_ sender: Any) {
190
+
191
+ let alert = UIAlertController(title: "New name", message: "Enter a new name", preferredStyle: .alert)
192
+
193
+
194
+
195
+ let saveAction = UIAlertAction(title: "Save", style: .default) { (action) in
196
+
197
+ let textField = alert.textFields?.first
198
+
199
+ self.saveName(name: textField!.text!)
200
+
201
+ self.tableView.reloadData()
202
+
203
+ }
204
+
205
+
206
+
207
+ let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
208
+
209
+
210
+
211
+ alert.addTextField(configurationHandler: nil)
212
+
213
+ alert.addAction(saveAction)
214
+
215
+ alert.addAction(cancelAction)
216
+
217
+
218
+
219
+ present(alert, animated: true, completion: nil)
220
+
221
+ }
222
+
223
+
224
+
225
+ func saveName(name: String){
226
+
227
+ let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
228
+
229
+
230
+
231
+ let person = Person(entity: Person.entity(), insertInto: context)
232
+
233
+
234
+
235
+ person.setValue(name, forKey: "name")
236
+
237
+
238
+
239
+ do{
240
+
241
+ try context.save()
242
+
243
+ people.append(person)
244
+
245
+ } catch let error as NSError {
246
+
247
+ print("Could not save \(error), \(error.userInfo)")
248
+
249
+ }
250
+
251
+ }
252
+
253
+ */
254
+
183
255
  override func viewWillAppear(_ animated: Bool) {
184
256
 
185
257
  super.viewWillAppear(animated)
186
258
 
187
-
259
+
188
260
 
189
261
  let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
190
262
 
@@ -218,4 +290,6 @@
218
290
 
219
291
 
220
292
 
293
+
294
+
221
295
  ```

1

やりたいことの編集

2017/05/28 16:11

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -4,13 +4,9 @@
4
4
 
5
5
  ![イメージ説明](fecc9b8382488c0c3f2723c70fc2f369.jpeg)
6
6
 
7
-
8
-
9
- 買ったものリストの隣の追加ボタンをタップ時にアラートが表示され、大まかなタイトルを入力できるようにし、それと同時に追加ボタンが追加されるようにしたいです
10
-
11
- 追加ボタンをタップ時に詳細内容を入力できるようにアラートを起動させて入力できるようにしたいです
7
+ のようにアラートを追加した時に入力した内容とさらにcellを追加できるボタンを追加できるようにしたいです
12
-
13
- また詳細の内容が追加される時は自動的に番号振ってある状態にしたいです。
8
+
9
+
14
10
 
15
11
 
16
12
 
@@ -223,5 +219,3 @@
223
219
 
224
220
 
225
221
  ```
226
-
227
- このようにcellに名前をつけて追加までは出来るのですが本来自分が行いことと違い、またいろいろ試行錯誤してみたのですがどうしてもうまく出来ないのでアドバイスまたは教えて頂けると助かります。