回答編集履歴
3
バグ修正2
test
CHANGED
@@ -262,7 +262,7 @@
|
|
262
262
|
|
263
263
|
|
264
264
|
|
265
|
-
protocol SecondViewControllerDelegate {
|
265
|
+
@objc protocol SecondViewControllerDelegate {
|
266
266
|
|
267
267
|
func didGetMemo(title:String, _ honbun:String)
|
268
268
|
|
@@ -280,7 +280,7 @@
|
|
280
280
|
|
281
281
|
|
282
282
|
|
283
|
-
var delegate:SecondViewControllerDelegate!
|
283
|
+
weak var delegate:SecondViewControllerDelegate!
|
284
284
|
|
285
285
|
var memoTitle:String = ""
|
286
286
|
|
@@ -369,5 +369,3 @@
|
|
369
369
|
変数名等が気に入らないものも勝手に変更しましたが、Storyboardと接続しているプロパティ名やメソッド名は変えないようにしておきましたので、そのままコードを差し替えるだけでStoryboardとちゃんと接続して動作できるはずです。
|
370
370
|
|
371
371
|
動作確認できたら、どう違うのかコードを比較してみてください。
|
372
|
-
|
373
|
-
|
2
バグ修正
test
CHANGED
@@ -154,13 +154,11 @@
|
|
154
154
|
|
155
155
|
to_edit.delegate = self
|
156
156
|
|
157
|
-
let e
|
157
|
+
if let indexPath = memoListView.indexPathForSelectedRow where indexPath.row < titleList.count {
|
158
|
-
|
159
|
-
|
158
|
+
|
160
|
-
|
161
|
-
to_edit.memoTitle = titleList[e
|
159
|
+
to_edit.memoTitle = titleList[indexPath.row]
|
162
|
-
|
160
|
+
|
163
|
-
to_edit.memoHonbun = honbunList[e
|
161
|
+
to_edit.memoHonbun = honbunList[indexPath.row]
|
164
162
|
|
165
163
|
}
|
166
164
|
|
@@ -172,13 +170,11 @@
|
|
172
170
|
|
173
171
|
func didGetMemo(title: String, _ honbun: String) {
|
174
172
|
|
175
|
-
let e
|
173
|
+
if let indexPath = memoListView.indexPathForSelectedRow where indexPath.row < titleList.count {
|
176
|
-
|
177
|
-
|
174
|
+
|
178
|
-
|
179
|
-
titleList[e
|
175
|
+
titleList[indexPath.row] = title
|
180
|
-
|
176
|
+
|
181
|
-
honbunList[e
|
177
|
+
honbunList[indexPath.row] = honbun
|
182
178
|
|
183
179
|
} else {
|
184
180
|
|
1
回答追加
test
CHANGED
@@ -27,3 +27,351 @@
|
|
27
27
|
`memoList2.removeAtIndex(indexPath.row)`
|
28
28
|
|
29
29
|
も漏れていると思います。
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
---
|
34
|
+
|
35
|
+
(6/16 23:30追記)
|
36
|
+
|
37
|
+
私だったらこうするというサンプルコードを作ってみました。
|
38
|
+
|
39
|
+
なお、画面遷移のやり方はpushViewControllerでもセグエでもどっちでもいいんですが、セグエを使う場合、Storyboardの設定を説明するのが面倒なので、最初の質問でやっていたpushViewControllerで遷移する方式でサンプルを作っています。
|
40
|
+
|
41
|
+
それと、もともとセルの追加をどうやるつもりだったのか説明がなかったので、テーブルの最後に「メモ追加」セルを加え、それをタップしたら編集画面に移行して入力したデータが追加されるようにしました。
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
(サンプルコード)
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
###memo_list.swift
|
50
|
+
|
51
|
+
```swift
|
52
|
+
|
53
|
+
import UIKit
|
54
|
+
|
55
|
+
class memo_list: UIViewController,UITableViewDelegate, UITableViewDataSource, SecondViewControllerDelegate {
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
@IBOutlet weak var memoListView: UITableView!
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
let userDefaults = NSUserDefaults.standardUserDefaults()
|
64
|
+
|
65
|
+
var titleList: [String]!
|
66
|
+
|
67
|
+
var honbunList: [String]!
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
override func viewDidLoad() {
|
72
|
+
|
73
|
+
super.viewDidLoad()
|
74
|
+
|
75
|
+
// 保存したデータ読み込み
|
76
|
+
|
77
|
+
titleList = userDefaults.stringArrayForKey("dly") ?? []
|
78
|
+
|
79
|
+
honbunList = userDefaults.stringArrayForKey("dly3") ?? []
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
// 配列の個数が合わない場合はリセットする
|
84
|
+
|
85
|
+
if titleList.count != honbunList.count {
|
86
|
+
|
87
|
+
titleList = []
|
88
|
+
|
89
|
+
honbunList = []
|
90
|
+
|
91
|
+
NSLog("memoListとmemoList2に不整合が生じたので両方リセットしました")
|
92
|
+
|
93
|
+
userDefaults.removeObjectForKey("dly")
|
94
|
+
|
95
|
+
userDefaults.removeObjectForKey("dly3")
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
// 編集ボタンを設定
|
100
|
+
|
101
|
+
navigationItem.rightBarButtonItem = editButtonItem()
|
102
|
+
|
103
|
+
// 編集モードでのセル選択可とする
|
104
|
+
|
105
|
+
memoListView.allowsSelectionDuringEditing = true
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
override func setEditing(editing: Bool, animated: Bool) {
|
112
|
+
|
113
|
+
super.setEditing(editing, animated: animated)
|
114
|
+
|
115
|
+
memoListView.editing = editing
|
116
|
+
|
117
|
+
}
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
122
|
+
|
123
|
+
return titleList.count+1
|
124
|
+
|
125
|
+
}
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
|
130
|
+
|
131
|
+
let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell")
|
132
|
+
|
133
|
+
if indexPath.row < titleList.count {
|
134
|
+
|
135
|
+
cell.textLabel?.text = titleList[indexPath.row]
|
136
|
+
|
137
|
+
cell.detailTextLabel?.text = honbunList[indexPath.row]
|
138
|
+
|
139
|
+
} else {
|
140
|
+
|
141
|
+
cell.textLabel?.text = "(メモ追加)"
|
142
|
+
|
143
|
+
}
|
144
|
+
|
145
|
+
return cell
|
146
|
+
|
147
|
+
}
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
|
152
|
+
|
153
|
+
let to_edit = self.storyboard?.instantiateViewControllerWithIdentifier("memo") as! SecondViewController
|
154
|
+
|
155
|
+
to_edit.delegate = self
|
156
|
+
|
157
|
+
let editRow = memoListView.indexPathForSelectedRow!.row
|
158
|
+
|
159
|
+
if editRow < titleList.count {
|
160
|
+
|
161
|
+
to_edit.memoTitle = titleList[editRow]
|
162
|
+
|
163
|
+
to_edit.memoHonbun = honbunList[editRow]
|
164
|
+
|
165
|
+
}
|
166
|
+
|
167
|
+
navigationController?.pushViewController(to_edit, animated: true)
|
168
|
+
|
169
|
+
}
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
func didGetMemo(title: String, _ honbun: String) {
|
174
|
+
|
175
|
+
let editRow = memoListView.indexPathForSelectedRow!.row
|
176
|
+
|
177
|
+
if editRow < titleList.count {
|
178
|
+
|
179
|
+
titleList[editRow] = title
|
180
|
+
|
181
|
+
honbunList[editRow] = honbun
|
182
|
+
|
183
|
+
} else {
|
184
|
+
|
185
|
+
titleList.append(title)
|
186
|
+
|
187
|
+
honbunList.append(honbun)
|
188
|
+
|
189
|
+
}
|
190
|
+
|
191
|
+
userDefaults.setObject(titleList, forKey: "dly")
|
192
|
+
|
193
|
+
userDefaults.setObject(honbunList, forKey: "dly3")
|
194
|
+
|
195
|
+
//テーブル更新
|
196
|
+
|
197
|
+
memoListView.reloadData()
|
198
|
+
|
199
|
+
}
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
|
204
|
+
|
205
|
+
if (indexPath.row < titleList.count) {
|
206
|
+
|
207
|
+
return .Delete
|
208
|
+
|
209
|
+
} else {
|
210
|
+
|
211
|
+
return .Insert
|
212
|
+
|
213
|
+
}
|
214
|
+
|
215
|
+
}
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
if editingStyle == .Delete {
|
224
|
+
|
225
|
+
//指定したセルの番号の配列番号を削除する
|
226
|
+
|
227
|
+
titleList.removeAtIndex(indexPath.row)
|
228
|
+
|
229
|
+
honbunList.removeAtIndex(indexPath.row)
|
230
|
+
|
231
|
+
// セルを削除
|
232
|
+
|
233
|
+
tableView.deleteRowsAtIndexPaths([NSIndexPath(forRow: indexPath.row, inSection: 0)], withRowAnimation: UITableViewRowAnimation.Automatic)
|
234
|
+
|
235
|
+
// 保存
|
236
|
+
|
237
|
+
userDefaults.setObject(titleList, forKey: "dly")
|
238
|
+
|
239
|
+
userDefaults.setObject(honbunList, forKey: "dly3")
|
240
|
+
|
241
|
+
} else if editingStyle == .Insert {
|
242
|
+
|
243
|
+
// セル追加画面起動
|
244
|
+
|
245
|
+
let to_edit = self.storyboard?.instantiateViewControllerWithIdentifier("memo") as! SecondViewController
|
246
|
+
|
247
|
+
to_edit.delegate = self
|
248
|
+
|
249
|
+
navigationController?.pushViewController(to_edit, animated: true)
|
250
|
+
|
251
|
+
}
|
252
|
+
|
253
|
+
}
|
254
|
+
|
255
|
+
}
|
256
|
+
|
257
|
+
```
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
###SecondViewController.swift
|
262
|
+
|
263
|
+
```swift
|
264
|
+
|
265
|
+
import UIKit
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
protocol SecondViewControllerDelegate {
|
270
|
+
|
271
|
+
func didGetMemo(title:String, _ honbun:String)
|
272
|
+
|
273
|
+
}
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
class SecondViewController: UIViewController,UITableViewDelegate,UITextFieldDelegate {
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
@IBOutlet weak var title_text: UITextField!
|
282
|
+
|
283
|
+
@IBOutlet weak var honbun_text: textview_custom!
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
var delegate:SecondViewControllerDelegate!
|
288
|
+
|
289
|
+
var memoTitle:String = ""
|
290
|
+
|
291
|
+
var memoHonbun:String = ""
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
override func viewDidLoad() {
|
298
|
+
|
299
|
+
super.viewDidLoad()
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
title_text.delegate = self
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
// 受け取ったタイトルとテキストを表示
|
308
|
+
|
309
|
+
title_text.text = memoTitle
|
310
|
+
|
311
|
+
honbun_text.text = memoHonbun
|
312
|
+
|
313
|
+
}
|
314
|
+
|
315
|
+
|
316
|
+
|
317
|
+
|
318
|
+
|
319
|
+
//保存ボタンアクション
|
320
|
+
|
321
|
+
@IBAction func save2(sender: AnyObject) {
|
322
|
+
|
323
|
+
delegate.didGetMemo(title_text.text!, honbun_text.text)
|
324
|
+
|
325
|
+
navigationController?.popViewControllerAnimated(true)
|
326
|
+
|
327
|
+
}
|
328
|
+
|
329
|
+
|
330
|
+
|
331
|
+
//キーボードを下げる処理
|
332
|
+
|
333
|
+
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
|
334
|
+
|
335
|
+
|
336
|
+
|
337
|
+
if(title_text.isFirstResponder()){
|
338
|
+
|
339
|
+
title_text.resignFirstResponder()
|
340
|
+
|
341
|
+
}
|
342
|
+
|
343
|
+
if(honbun_text.isFirstResponder()){
|
344
|
+
|
345
|
+
honbun_text.resignFirstResponder()
|
346
|
+
|
347
|
+
}
|
348
|
+
|
349
|
+
|
350
|
+
|
351
|
+
}
|
352
|
+
|
353
|
+
|
354
|
+
|
355
|
+
//保存処理をしないで、テーブルビューページに移動する処理
|
356
|
+
|
357
|
+
@IBAction func listbotton(sender: AnyObject) {
|
358
|
+
|
359
|
+
navigationController?.popViewControllerAnimated(true)
|
360
|
+
|
361
|
+
}
|
362
|
+
|
363
|
+
}
|
364
|
+
|
365
|
+
```
|
366
|
+
|
367
|
+
|
368
|
+
|
369
|
+
memo_list.swiftとSecondViewController.swiftを上記の内容に差し替えてみてください。
|
370
|
+
|
371
|
+
|
372
|
+
|
373
|
+
変数名等が気に入らないものも勝手に変更しましたが、Storyboardと接続しているプロパティ名やメソッド名は変えないようにしておきましたので、そのままコードを差し替えるだけでStoryboardとちゃんと接続して動作できるはずです。
|
374
|
+
|
375
|
+
動作確認できたら、どう違うのかコードを比較してみてください。
|
376
|
+
|
377
|
+
|