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

質問編集履歴

5

コードの変更

2017/10/28 13:19

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -1,5 +1,4 @@
1
- ![イメージ説明](6a91dc1908cdefea46c70606d8607669.png)**やりたいこと**
1
+ ![イメージ説明](6a91dc1908cdefea46c70606d8607669.png)
2
- 各cellのタイトルだけを左から右にスワイプして削除できるようにしたいです。
3
2
 
4
3
  **出来たことと今のコード**
5
4
 
@@ -24,8 +23,8 @@
24
23
  mytableView.dataSource = self
25
24
 
26
25
  for name in initialItems {
27
- kei.append(Item(name: name, deletable: false,completable: false))
28
- kei.append(Item(name: name, deletable: false, completable: false))
26
+ kei.append(Item(name: name, deletable: false,deletecell: false, completable: false,cellcolor: false))
27
+ kei.append(Item(name: name, deletable: false, deletecell: false, completable: false,cellcolor: false))
29
28
  }
30
29
 
31
30
  }
@@ -47,9 +46,9 @@
47
46
 
48
47
  for textField in textFields {
49
48
  //self.items.append(Item(name: textField.text!))
50
- self.kei.append(Item(name: textField.text!,deletable: false,completable: false))
49
+ self.kei.append(Item(name: textField.text!,deletable: true, deletecell: true,completable: false, cellcolor: true))
51
50
  }
52
- self.kei.append(Item(name: "cell 追加 ボタン",deletable: true,completable: true))
51
+ self.kei.append(Item(name: textField.text!,deletable: true, deletecell: true,completable: false, cellcolor: true))
53
52
  self.mytableView.reloadData()
54
53
 
55
54
  }
@@ -137,7 +136,7 @@
137
136
 
138
137
 
139
138
  for textField in textFields {
140
- self.kei.insert(Item(name: textField.text!,deletable: false,completable: false), at: indexPath.row)
139
+ self.kei[indexPath.row] = (Item(name: textField.text!,deletable: false, deletecell: true,completable: false, cellcolor: false)), at: indexPath.row)
141
140
  }
142
141
  self.mytableView.reloadData()
143
142
 
@@ -192,7 +191,7 @@
192
191
  if let textFields = alert.textFields {
193
192
 
194
193
  for textField in textFields {
195
- self.kei[indexPath.row] = (Item(name: textField.text!,deletable: true,completable :true))
194
+ self.kei[indexPath.row] = (Item(name: textField.text!,deletable: false, deletecell: true,completable: false, cellcolor: false))
196
195
  //self.kei.append(textField.text!)
197
196
  }
198
197
  self.mytableView.reloadData()
@@ -265,7 +264,7 @@
265
264
  if let textFields = alert.textFields {
266
265
 
267
266
  for textField in textFields {
268
- self.kei[indexPath.row] = (Item(name: textField.text!,deletable: true,completable: true))
267
+ self.kei[indexPath.row] = (Item(name: textField.text!,deletable: false, deletecell: true,completable: false, cellcolor: false))
269
268
 
270
269
  }
271
270
  self.mytableView.reloadData()

4

画像の追加

2017/10/28 13:19

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -1,4 +1,4 @@
1
- **やりたいこと**
1
+ ![イメージ説明](6a91dc1908cdefea46c70606d8607669.png)**やりたいこと**
2
2
  各cellのタイトルだけを左から右にスワイプして削除できるようにしたいです。
3
3
 
4
4
  **出来たことと今のコード**

3

コードの変更

2017/10/27 01:55

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -1,225 +1,237 @@
1
+ **やりたいこと**
2
+ 各cellのタイトルだけを左から右にスワイプして削除できるようにしたいです。
3
+
4
+ **出来たことと今のコード**
5
+
1
- ```swift
6
+ ```ここに言語を入力
2
7
  import UIKit
3
8
 
4
9
  class ViewController: UIViewController, UITableViewDelegate,UITableViewDataSource {
5
10
 
6
11
  @IBOutlet weak var mytableView: UITableView!
12
+
13
+
7
-
14
+ let initialItems: [String] = []
15
+
8
16
  var kei: [Item] = Array()
9
17
 
10
-
11
-
18
+
19
+
12
20
  override func viewDidLoad() {
13
21
  super.viewDidLoad()
14
22
 
15
23
  mytableView.delegate = self
16
24
  mytableView.dataSource = self
25
+
17
-
26
+ for name in initialItems {
27
+ kei.append(Item(name: name, deletable: false,completable: false))
28
+ kei.append(Item(name: name, deletable: false, completable: false))
29
+ }
30
+
18
31
  }
19
-
20
-
32
+
33
+
34
+
21
35
  @IBAction func addbtr(_ sender: Any) {
22
-
23
-
36
+
37
+
24
38
  let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert)
25
-
26
-
39
+
40
+
27
41
  let okAction = UIAlertAction(title: "OK", style: .default, handler: {
28
42
  (action:UIAlertAction!) -> Void in
29
-
30
-
43
+
44
+
31
45
  if let textFields = alert.textFields {
32
-
46
+
47
+
33
48
  for textField in textFields {
34
- self.kei.append(Item(name: textField.text!))
49
+ //self.items.append(Item(name: textField.text!))
35
- self.kei.append(Item(name: "cell 追加 ボタン"))
50
+ self.kei.append(Item(name: textField.text!,deletable: false,completable: false))
36
- //(Item(name: textField.text!)
37
51
  }
52
+ self.kei.append(Item(name: "cell 追加 ボタン",deletable: true,completable: true))
38
53
  self.mytableView.reloadData()
39
-
54
+
40
55
  }
41
56
  })
42
57
  alert.addAction(okAction)
43
-
44
-
58
+
59
+
45
60
  let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
46
61
  alert.addAction(cancelAction)
47
-
48
-
62
+
63
+
49
64
  alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in
50
65
  textField.placeholder = "テキスト"
51
66
  })
52
-
53
-
67
+
68
+
54
69
  alert.view.setNeedsLayout()
55
-
56
70
  self.present(alert, animated: true, completion: nil)
57
-
71
+
58
72
  }
59
-
73
+
60
74
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
61
75
  return kei.count
62
76
  }
63
-
64
-
77
+
78
+
65
79
  func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
66
-
80
+
67
81
  switch indexPath.row {
68
82
  case 1:
69
83
  return indexPath;
84
+
70
85
  default:
71
86
  return nil;
72
87
  }
73
88
  }
74
-
89
+
75
90
  func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
76
91
  return indexPath
77
92
  }
78
-
93
+
79
94
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
80
-
95
+
81
96
  let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
82
-
97
+ //追加した内容に対して
83
98
  let item = kei[indexPath.row]
84
99
  cell.textLabel?.text = item.name
85
- cell.textLabel?.textColor = UIColor.red
100
+
86
- cell.backgroundColor = UIColor.black
87
101
 
88
-
89
-
90
102
  if cell.textLabel?.text != "cell 追加 ボタン" {
91
-      cell.accessoryType = item.complete ? .checkmark : .none
103
+ cell.accessoryType = item.complete ? .checkmark : .none
92
104
  cell.textLabel?.textColor = UIColor.black
93
105
  cell.backgroundColor = UIColor.yellow
94
-
106
+ }else {
107
+ cell.accessoryType = .none
108
+ cell.textLabel?.textColor = UIColor.red
109
+ cell.backgroundColor = UIColor.black
95
- }
110
+ }
111
+
96
112
  if indexPath.row == 0 {
97
113
  cell.textLabel?.textColor = UIColor.black
98
114
  cell.backgroundColor = UIColor.white
115
+ cell.accessoryType = .none
99
116
  }
100
-
117
+
101
118
  return cell
102
119
  }
103
-
104
120
  func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) -> IndexPath? {
105
121
 
106
122
  let cell = tableView.cellForRow(at: indexPath)
107
-
123
+
108
124
  if cell?.textLabel?.text != "cell 追加 ボタン"{
109
125
  kei[indexPath.row].toggle()
110
126
  tableView.deselectRow(at: indexPath, animated: true)
127
+ tableView.reloadRows(at: [indexPath], with: .none)
111
128
  return nil
112
129
  }else {
113
130
  let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert)
114
-
131
+
132
+
115
133
  let okAction = UIAlertAction(title: "OK", style: .default, handler: {
116
134
  (action:UIAlertAction!) -> Void in
117
-
118
-
135
+
119
136
  if let textFields = alert.textFields {
120
-
121
-
137
+
138
+
122
139
  for textField in textFields {
123
- self.kei.insert(Item(name: textField.text!), at: indexPath.row)
140
+ self.kei.insert(Item(name: textField.text!,deletable: false,completable: false), at: indexPath.row)
124
-
125
141
  }
126
142
  self.mytableView.reloadData()
127
-
143
+
128
144
  }
129
145
  })
130
146
  alert.addAction(okAction)
131
-
147
+
148
+
132
149
  let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
133
150
  alert.addAction(cancelAction)
134
-
151
+
152
+
135
153
  alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in
136
154
  textField.placeholder = "テキスト"
137
155
  })
138
-
139
-
156
+
157
+
140
- alert.view.setNeedsLayout()
158
+ alert.view.setNeedsLayout() /
141
-
142
-
143
159
  self.present(alert, animated: true, completion: nil)
144
-
160
+
145
161
  return indexPath
146
162
  }
147
-
163
+
148
164
  }
149
-
150
-
151
-
165
+
166
+
167
+
152
168
  func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
153
169
  if editingStyle == .delete {
154
170
  self.kei.remove(at: indexPath.row)
155
171
  tableView.deleteRows(at: [indexPath], with: .fade)
156
172
  }
157
173
  }
158
-
174
+
159
- func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
175
+ //func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
160
-
176
+ func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
161
- let delete = UITableViewRowAction(style: .normal, title: "削除") {ation, index in
177
+ let delete = UIContextualAction(style: .normal,title: "削除", handler: { (action: UIContextualAction, view: UIView, success :(Bool) -> Void) in
162
- self.kei.remove(at: (indexPath.row))
178
+ self.kei.remove(at: indexPath.row)
163
179
  tableView.deleteRows(at: [indexPath], with: .fade)
180
+ success(true)
164
- }
181
+ })
165
- delete.backgroundColor = .red
182
+
166
-
167
- let edit = UITableViewRowAction(style: .normal, title: "編集") { action, index in
183
+ let edit = UIContextualAction(style: .normal,title: "編集", handler: { (action: UIContextualAction, view: UIView, success :(Bool) -> Void) in
168
-
184
+
169
185
  let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert)
170
-
171
-
186
+
187
+
172
188
  let okAction = UIAlertAction(title: "OK", style: .default, handler: {
173
189
  (action:UIAlertAction!) -> Void in
174
-
175
-
190
+
191
+
176
192
  if let textFields = alert.textFields {
177
-
178
-
193
+
179
194
  for textField in textFields {
180
- self.kei[indexPath.row] = (Item(name: textField.text!))
195
+ self.kei[indexPath.row] = (Item(name: textField.text!,deletable: true,completable :true))
181
196
  //self.kei.append(textField.text!)
182
197
  }
183
198
  self.mytableView.reloadData()
184
-
199
+
185
200
  }
186
201
  })
187
202
  alert.addAction(okAction)
188
-
189
-
203
+
204
+
190
205
  let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
191
206
  alert.addAction(cancelAction)
192
-
193
-
207
+
208
+
194
209
  alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in
195
210
  textField.placeholder = "テキスト"
196
211
  })
197
-
198
-
212
+
213
+
199
214
  alert.view.setNeedsLayout()
200
-
201
-
202
215
  self.present(alert, animated: true, completion: nil)
203
216
  print("edit")
217
+
218
+ success(true)
204
- }
219
+ })
205
- edit.backgroundColor = .orange
206
-
207
- return [delete,edit]
208
- }
209
-
210
- //Method Swipe from left
211
- //左から右へスワイプ
212
- func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) ->UISwipeActionsConfiguration? {
213
220
 
221
+ delete.backgroundColor = .red
222
+ edit.backgroundColor = .blue
223
+
214
- let delete = UITableViewRowAction(style: .normal, title: "削除") {ation, index in
224
+ let swipeAction = UISwipeActionsConfiguration(actions: [delete])
215
- self.kei.remove(at: (indexPath.row))
216
- tableView.deleteRows(at: [indexPath], with: .fade)
225
+ swipeAction.performsFirstActionWithFullSwipe = false
226
+
227
+ return UISwipeActionsConfiguration(actions: [delete,edit])
217
228
  }
229
+
230
+
231
+
218
- return [delete]
232
+ // trueを返すことでCellのアクションを許可しています
219
- }
220
-
221
233
  func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
222
-
234
+
223
235
  if kei.count > (indexPath.row + 1) {
224
236
  return true
225
237
  }else{
@@ -229,34 +241,60 @@
229
241
 
230
242
  override func didReceiveMemoryWarning() {
231
243
  super.didReceiveMemoryWarning()
232
- // Dispose of any resources that can be recreated.
244
+
233
245
  }
234
-
235
- }
236
- ```
237
-
238
- 右からのスワイプに関しては色々ネットにやり方が載っているのですが左から右にスワイプする方法に関しるやり方があまり載っていなくappleの公式ドキュメントやQiitaを参考に私なりにコードを書いてみたのですがエラーが出ておりどうすればできるのかを知りたいです。
239
-
240
- **やりたいこと**
241
- 各cellの先頭だけを左から右にスワイプして削除できるようにしたいです。
242
-
243
- **出来たこと**
244
- 左から右に関するスワイプのコード
245
- ```
246
- //左から右へスワイプ
246
+ //左から右へスワイプ
247
247
  func tableView(_ tableView: UITableView,
248
248
  leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
249
249
 
250
- let delete = UIContextualAction(style: .normal,title: "削除",handler: { (action: UIContextualAction, view: UIView, success :(Bool) -> Void) in
250
+ let delete = UIContextualAction(style: .normal,title: "削除", handler: { (action: UIContextualAction, view: UIView, success :(Bool) -> Void) in
251
- self.kei.remove(at: (indexPath.row))
251
+ self.kei.remove(at: indexPath.row)
252
252
  tableView.deleteRows(at: [indexPath], with: .fade)
253
+ success(true)
253
254
  })
254
255
 
256
+ let edit = UIContextualAction(style: .normal,title: "編集", handler: { (action: UIContextualAction, view: UIView, success :(Bool) -> Void) in
257
+
258
+ let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert)
259
+
260
+ // OKボタンの設定
261
+ let okAction = UIAlertAction(title: "OK", style: .default, handler: {
262
+ (action:UIAlertAction!) -> Void in
263
+
264
+ // OKを押した時入力されていたテキストを表示
265
+ if let textFields = alert.textFields {
266
+
267
+ for textField in textFields {
268
+ self.kei[indexPath.row] = (Item(name: textField.text!,deletable: true,completable: true))
269
+
270
+ }
271
+ self.mytableView.reloadData()
272
+
273
+ }
274
+ })
275
+ alert.addAction(okAction)
276
+
277
+ let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
278
+ alert.addAction(cancelAction)
279
+
280
+
281
+ alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in
282
+ textField.placeholder = "テキスト"
283
+ })
284
+ alert.view.setNeedsLayout()
285
+ self.present(alert, animated: true, completion: nil)
286
+ print("edit")
287
+
288
+ success(true)
289
+ })
290
+
291
+ delete.backgroundColor = .orange
292
+ edit.backgroundColor = .blue
293
+
255
- return UISwipeActionsConfiguration(actions: [delete])
294
+ return UISwipeActionsConfiguration(actions: [delete,edit])
295
+
256
296
  }
297
+
298
+ }
257
299
 
258
- ```
300
+ ```
259
- ![イメージ説明](799b1aa83cb88d590fe28f1279181229.png)
260
-
261
- まずios11にしてからコードを上記のようにしたら左から右にスワイプして削除できるようになりました。
262
- しかし、スワイプして削除してもスワイプした状態が続いていてそれを戻すには自分でまた元に戻さないといけないのでスワイプして削除が完了したらそのままスワイプの状態がなくなるようにしたいです。

2

スワイプに関する処理

2017/10/24 05:06

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -240,21 +240,23 @@
240
240
  **やりたいこと**
241
241
  各cellの先頭だけを左から右にスワイプして削除できるようにしたいです。
242
242
 
243
+ **出来たこと**
243
244
  左から右に関するスワイプのコード
244
245
  ```
245
- //Method Swipe from left
246
- //左から右へスワイプ
246
+ //左から右へスワイプ
247
+ func tableView(_ tableView: UITableView,
247
- func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) ->UISwipeActionsConfiguration? {
248
+ leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
248
-
249
+
249
- let delete = UITableViewRowAction(style: .normal, title: "削除") {ation, index in
250
+ let delete = UIContextualAction(style: .normal,title: "削除",handler: { (action: UIContextualAction, view: UIView, success :(Bool) -> Void) in
250
- self.kei.remove(at: (indexPath.row))
251
+ self.kei.remove(at: (indexPath.row))
251
- tableView.deleteRows(at: [indexPath], with: .fade)
252
+ tableView.deleteRows(at: [indexPath], with: .fade)
252
- }
253
+ })
254
+
253
- return [delete]
255
+ return UISwipeActionsConfiguration(actions: [delete])
254
256
  }
255
257
 
256
- }
258
+ ```
259
+ ![イメージ説明](799b1aa83cb88d590fe28f1279181229.png)
257
260
 
258
- ```
259
- **エラー内容**
260
- ![イメジ説明](010892237d423ec1a6e9f191f65f0d85.png)
261
+ まずios11にしてからコドを上記のようにしたら左から右にスワイプして削除できるようになりました。
262
+ しかし、スワイプして削除してもスワイプした状態が続いていてそれを戻すには自分でまた元に戻さないといけないのでスワイプして削除が完了したらそのままスワイプの状態がなくなるようにしたいです。

1

コードの変更

2017/10/22 05:18

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -209,17 +209,15 @@
209
209
 
210
210
  //Method Swipe from left
211
211
  //左から右へスワイプ
212
- func tableView(_ tableView: UITableView,leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
212
+ func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) ->UISwipeActionsConfiguration? {
213
-
213
+
214
- let removeAction = UIContextualAction(style: .normal,title: "削除",handler: { (action: UIContextualAction, view: UIView, success :(Bool) -> Void)in success(true)
214
+ let delete = UITableViewRowAction(style: .normal, title: "削除") {ation, index in
215
+ self.kei.remove(at: (indexPath.row))
216
+ tableView.deleteRows(at: [indexPath], with: .fade)
215
- })
217
+ }
216
- removeAction.image = UIImage(named: "trash")
217
- removeAction.backgroundColor = .red
218
+ return [delete]
218
-
219
- return UISwipeActionsConfiguration(actions: [removeAction])
220
219
  }
221
220
 
222
-
223
221
  func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
224
222
 
225
223
  if kei.count > (indexPath.row + 1) {
@@ -246,14 +244,15 @@
246
244
  ```
247
245
  //Method Swipe from left
248
246
  //左から右へスワイプ
249
- func tableView(_ tableView: UITableView,leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
247
+ func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) ->UISwipeActionsConfiguration? {
248
+
249
+ let delete = UITableViewRowAction(style: .normal, title: "削除") {ation, index in
250
+ self.kei.remove(at: (indexPath.row))
251
+ tableView.deleteRows(at: [indexPath], with: .fade)
252
+ }
253
+ return [delete]
254
+ }
250
255
 
251
- let removeAction = UIContextualAction(style: .normal,title: "削除",handler: { (action: UIContextualAction, view: UIView, success :(Bool) -> Void)in success(true)
252
- })
253
- removeAction.image = UIImage(named: "trash")
254
- removeAction.backgroundColor = .red
255
-
256
- return UISwipeActionsConfiguration(actions: [removeAction])
257
256
  }
258
257
 
259
258
  ```