teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

書式の改善

2019/03/21 08:37

投稿

hik_
hik_

スコア42

title CHANGED
File without changes
body CHANGED
@@ -140,6 +140,8 @@
140
140
  ```
141
141
 
142
142
  ### 追記 3月21日
143
+ 現状のコードです。
144
+ ご教示頂けると幸いです。
143
145
  ```ここに言語を入力
144
146
  import UIKit
145
147
 

2

情報の追加

2019/03/21 08:37

投稿

hik_
hik_

スコア42

title CHANGED
File without changes
body CHANGED
@@ -138,6 +138,145 @@
138
138
 
139
139
  }
140
140
  ```
141
+
142
+ ### 追記 3月21日
143
+ ```ここに言語を入力
144
+ import UIKit
145
+
146
+ // memoTextFieldに入力された文字を格納する変数
147
+ var memoText = [String]()
148
+
149
+ // 再編集の場合の記事番号、-1なら新規作成
150
+ var honIndex = -1
151
+ var honRow: Int = honIndex
152
+
153
+
154
+
155
+ class ViewController: UIViewController {
156
+
157
+ // 変数を作成
158
+ var aDate: String = ""
159
+
160
+ @IBOutlet weak var memoTextView: UITextView!
161
+
162
+
163
+ override func viewDidLoad() {
164
+ super.viewDidLoad()
165
+
166
+ // memoTextViewにaDateを入れる
167
+ memoTextView.text = aDate
168
+ }
169
+
170
+
171
+ @IBAction func setButton(_ sender: Any) {
172
+
173
+ if honRow == honIndex {
174
+ memoText.append(memoTextView.text!)
175
+ } else {
176
+ memoText[honRow] = memoTextView.text!
177
+ }
178
+ honRow = honIndex
179
+
180
+
181
+ // memoTextにmemoTextViewに入力された文字を格納
182
+ memoText.append(memoTextView.text!)
183
+
184
+
185
+ // memoTextをUserDefaultsに保存
186
+ UserDefaults.standard.set(memoText, forKey: "memoTextKey")
187
+
188
+ }
189
+
190
+ }
191
+
192
+ ```
193
+
194
+ ```ここに言語を入力
195
+ import UIKit
196
+
197
+ class TableViewController: UITableViewController {
198
+
199
+ // 変数
200
+ var honMemo: String = ""
201
+
202
+
203
+ override func viewDidLoad() {
204
+ super.viewDidLoad()
205
+
206
+
207
+ if UserDefaults.standard.object(forKey: "memoTextKey") != nil {
208
+ memoText = UserDefaults.standard.object(forKey: "memoTextKey") as! [String]
209
+ }
210
+
211
+
212
+ }
213
+
214
+ override func viewDidAppear(_ animated: Bool) {
215
+
216
+ // TableViewを開く度にtableViewを更新する
217
+ tableView.reloadData()
218
+ }
219
+
220
+
221
+
222
+
223
+ override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
224
+
225
+ // memoTextの数だけCell作成
226
+ return memoText.count
227
+ }
228
+
229
+ override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
230
+
231
+ // Cellの表示内容を設定
232
+ let Cell : UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath)
233
+
234
+
235
+
236
+ Cell.textLabel!.text = memoText[indexPath.row]
237
+
238
+ return Cell
239
+ }
240
+
241
+ // Cellを押した時の処理
242
+ override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
243
+
244
+
245
+ honMemo = memoText[indexPath.item]
246
+ performSegue(withIdentifier: "Segue", sender: nil)
247
+
248
+
249
+
250
+
251
+ }
252
+
253
+
254
+
255
+ override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
256
+ // 削除機能
257
+ let index = indexPath.row
258
+
259
+ memoText.remove(at: index)
260
+
261
+ // 削除した事を保存
262
+ UserDefaults.standard.set(memoText, forKey: "memoTextKey")
263
+ // tableViewを再読み込み
264
+ tableView.reloadData()
265
+
266
+
267
+
268
+ }
269
+
270
+
271
+ // ViewControllerへの引き継ぎ設定
272
+ override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
273
+ let vc = segue.destination as! ViewController
274
+ vc.aDate = honMemo
275
+ }
276
+
277
+
278
+ }
279
+ ```
141
280
  ### 試したこと
142
281
  現在TableViewCellなど解決出来そうな情報を探しているのですが、実装出来ていない状況です。
143
282
  ご教示頂けると幸いです。

1

情報の追加(画像)、タイトルの改善

2019/03/21 08:36

投稿

hik_
hik_

スコア42

title CHANGED
@@ -1,1 +1,1 @@
1
- TableViewのCellの編集
1
+ TableViewのCellが2個表示されてしまう
body CHANGED
@@ -2,6 +2,7 @@
2
2
  学習のためメモ帳アプリを作っていて、メモを作成して作成したメモをUserDefaultsで保存をする所まで作れたのですが、
3
3
  メモを作成したCellをタップして文字を編集すると、編集後のテキストがあるCellだけで良いのですが、編集前のCellと編集後のCellの2つが作成されてしまう(例 「お菓子を買う」 というCellをタップして、TextViewに「お菓子を食べる」と書き直して保存を押すと、「お菓子を買う」だったCellが「お菓子を食べる」に変われば良いのですが、Cell1個目が「お菓子を買う」,Cell2個目に編集後の「お菓子を食べる」と2つ作成されてしまいます。)
4
4
 
5
+ ![イメージ説明](5711cd78d464e67176134fc8e46aaefc.png)
5
6
 
6
7
  ### 該当のソースコード
7
8