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

質問編集履歴

2

コードを載せました

2020/04/18 02:14

投稿

sumire06
sumire06

スコア15

title CHANGED
File without changes
body CHANGED
@@ -13,9 +13,18 @@
13
13
  どこに問題があるか教えていただけると嬉しいです。
14
14
  よろしくお願いいたします。
15
15
 
16
+ ・・・追加
17
+ 現在、このようなコードになっております。
18
+ よろしくお願いいたします。
19
+
16
20
  ```swift
21
+ import UIKit
22
+ private let unselectedRow = -1
23
+
17
24
  class MemoViewController: UIViewController,UITableViewDataSource, UITextFieldDelegate,UITableViewDelegate {
18
25
 
26
+
27
+
19
28
  @IBOutlet weak var editMemoField: UITextField!
20
29
 
21
30
  @IBOutlet weak var memoListView: UITableView!
@@ -28,7 +37,8 @@
28
37
  var editRow: Int = unselectedRow
29
38
  var giveData: String = ""
30
39
 
40
+
31
- override func viewDidLoad() {
41
+ override func viewDidLoad() {
32
42
  super.viewDidLoad()
33
43
 
34
44
  memoListView.delegate = self
@@ -36,13 +46,17 @@
36
46
  memoListView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
37
47
  editMemoField.becomeFirstResponder()
38
48
  editMemoField.keyboardType = UIKeyboardType.emailAddress
39
-
49
+
50
+
51
+
40
52
  if let aaa = userDefaults.object(forKey: "memoList") {
41
53
  memoList = aaa as! Array<String>
42
54
  }
43
55
  }
56
+
57
+
44
- func textFieldShouldReturn(_ textField: UITextField) -> Bool {
58
+ func textFieldShouldReturn(_ textField: UITextField) -> Bool {
45
-
59
+ memoList.append(textField.text!)
46
60
  userDefaults.set(memoList, forKey: "memoList")
47
61
  userDefaults.synchronize()
48
62
 
@@ -55,7 +69,45 @@
55
69
  return true
56
70
 
57
71
  }
72
+
73
+ override func didReceiveMemoryWarning() {
74
+ super.didReceiveMemoryWarning()
75
+ // Dispose of any resources that can be recreated.
76
+ }
77
+
78
+
79
+
80
+
81
+
82
+ @IBAction func tapSubmitButton(_ sender: Any) {
83
+
84
+ applyMemo()
85
+
86
+ }
87
+ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
88
+
89
+ return 12
90
+ }
91
+
92
+ func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
93
+
94
+
95
+ let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath)
96
+
97
+
98
+ if indexPath.row >= memoList.count {
99
+ return cell
100
+ }
101
+
102
+ cell.textLabel?.text = memoList[indexPath.row]
103
+
104
+ return cell
105
+
106
+ }
107
+
108
+
109
+
58
- func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
110
+ func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
59
111
 
60
112
 
61
113
 
@@ -75,9 +127,41 @@
75
127
 
76
128
 
77
129
 
130
+ }else if indexPath.row == 1 {
131
+
132
+ performSegue(withIdentifier: "toCell1", sender: nil)
133
+
134
+
135
+
78
- }else if ・・・・・・
136
+ ]・・・・・・
137
+
138
+
79
-
139
+ }
140
+
141
+ override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
142
+
143
+ if segue.identifier == "toCell0" {
144
+
145
+ let vc = segue.destination as! Cell0ViewController
146
+ vc.receiveData = giveData
147
+
148
+ ・・・・・・
149
+ }
150
+
151
+
152
+
153
+ }
154
+
155
+
156
+
157
+ private func textFieldShouldReturn(textField: UITextField) -> Bool {
158
+ applyMemo()
159
+ return true
160
+ }
161
+
162
+
163
+
80
- func applyMemo() {
164
+ func applyMemo() {
81
165
  if editMemoField.text == nil {
82
166
  return
83
167
  }
@@ -90,7 +174,7 @@
90
174
  userDefaults.set(memoList, forKey: "memoList")
91
175
  userDefaults.synchronize()
92
176
 
93
- memoList = userDefaults.object(forKey: "memoList") as! Array<String>
177
+ memoList = userDefaults.object(forKey: "memoList") as! Array<String>
94
178
 
95
179
  }
96
180
  editMemoField.text = ""
@@ -100,12 +184,17 @@
100
184
 
101
185
 
102
186
  }
103
- func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
187
+ func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
188
+
189
+ if editingStyle == UITableViewCell.EditingStyle.delete{
104
- memoList.remove(at: indexPath.row)
190
+ memoList.remove(at: indexPath.row)
105
- tableView.deleteRows(at: [indexPath], with: UITableView.RowAnimation.automatic)
191
+ tableView.deleteRows(at : [indexPath], with: .automatic)
106
- }
192
+ }
107
-
193
+ }
108
194
 
195
+
196
+
197
+
109
198
  }
110
199
 
111
200
 

1

コードの追加、写真の追加

2020/04/18 02:14

投稿

sumire06
sumire06

スコア15

title CHANGED
File without changes
body CHANGED
@@ -1,10 +1,18 @@
1
- ### tableviewcellをスワイプして削除したい。遷移先のデータも削除したい。
1
+ ![イメージ説明](9168dd4ad855fd435cf5be109cf9826a.png)### tableviewcellをスワイプして削除したい。遷移先のデータも削除したい。
2
2
 
3
3
  調べたコードを入れるとスワイプの状態にはなりますがdeleteボタンを押すとクラッシュします。
4
4
  一番下のコードが追加したコードです。
5
5
 
6
- また、cellの情報を削除するとセルの遷移先の情報も削除されのかを教えていただけると嬉しいです。
6
+ また、cellの情報を削除するとセルの遷移先の情報も削除されのかを教えていただけると嬉しいです。
7
7
 
8
+
9
+ ・・追加
10
+
11
+ 追加したコードは問題ではなさそうでした。
12
+ 他のコードに問題があるのですね。
13
+ どこに問題があるか教えていただけると嬉しいです。
14
+ よろしくお願いいたします。
15
+
8
16
  ```swift
9
17
  class MemoViewController: UIViewController,UITableViewDataSource, UITextFieldDelegate,UITableViewDelegate {
10
18
 
@@ -47,6 +55,51 @@
47
55
  return true
48
56
 
49
57
  }
58
+ func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
59
+
60
+
61
+
62
+
63
+ if indexPath.row >= memoList.count {
64
+
65
+ return
66
+ }
67
+
68
+
69
+ editRow = indexPath.row
70
+ editMemoField.text = memoList[editRow]
71
+ giveData = memoList[indexPath.item]
72
+ if indexPath.row == 0 {
73
+
74
+ performSegue(withIdentifier: "toCell0", sender: nil)
75
+
76
+
77
+
78
+ }else if ・・・・・・・
79
+
80
+ func applyMemo() {
81
+ if editMemoField.text == nil {
82
+ return
83
+ }
84
+
85
+ if editRow == unselectedRow {
86
+ memoList.append(editMemoField.text!)
87
+
88
+ } else {
89
+ memoList[editRow] = editMemoField.text!
90
+ userDefaults.set(memoList, forKey: "memoList")
91
+ userDefaults.synchronize()
92
+
93
+ memoList = userDefaults.object(forKey: "memoList") as! Array<String>
94
+
95
+ }
96
+ editMemoField.text = ""
97
+ editRow = unselectedRow
98
+
99
+ memoListView.reloadData()
100
+
101
+
102
+ }
50
103
  func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
51
104
  memoList.remove(at: indexPath.row)
52
105
  tableView.deleteRows(at: [indexPath], with: UITableView.RowAnimation.automatic)