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

質問編集履歴

6

コードの追加

2017/10/04 08:18

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -271,4 +271,19 @@
271
271
  ```
272
272
 
273
273
  @IBAction func addbtr
274
- のなかのself.kei.append(textField.text!) をcell.textLabel?.text != "title"に当てはめればおそらく私のやりたい事が出来ると思うんですけどtextField.text!の場合""に入れても"cell 追加 ボタン"のようにチェックがつかないようにならないです。
274
+ のなかのself.kei.append(textField.text!) をcell.textLabel?.text != "title"に当てはめればおそらく私のやりたい事が出来ると思うんですけどtextField.text!の場合""に入れても"cell 追加 ボタン"のようにチェックがつかないようにならないです。
275
+
276
+ ```
277
+ func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) -> IndexPath? {
278
+
279
+ let cell = tableView.cellForRow(at: indexPath)
280
+
281
+ if indexPath.row == 0 {
282
+ return nil
283
+ }
284
+
285
+ if cell?.textLabel?.text != "cell 追加 ボタン"{
286
+ cell?.accessoryType = cell?.accessoryType == .checkmark ? .none : .checkmark
287
+ tableView.deselectRow(at: indexPath, animated: true)
288
+ return nil
289
+ ```

5

説明の追加

2017/10/04 08:18

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -271,4 +271,4 @@
271
271
  ```
272
272
 
273
273
  @IBAction func addbtr
274
- のなかのself.kei.append(textField.text!) をcell.textLabel?.text != "title"に当てはめればおそらく私のやりたい事が出来ると思うんですけどtextField.text!の場合""に入れても"cell 追加 ボタン"のようにチェックつかないようにならないです。
274
+ のなかのself.kei.append(textField.text!) をcell.textLabel?.text != "title"に当てはめればおそらく私のやりたい事が出来ると思うんですけどtextField.text!の場合""に入れても"cell 追加 ボタン"のようにチェックつかないようにならないです。

4

コードの追加

2017/10/04 02:18

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -254,10 +254,21 @@
254
254
 
255
255
  ```
256
256
 
257
+
258
+ **追記**
257
259
  ![イメージ説明](7030344f5bb0f7a975ef270b18ac45af.png)
258
260
 
259
261
  ```
260
262
  @IBAction func addbtr(_ sender: Any) {
261
263
     self.kei.append(textField.text!)
262
264
  self.kei.append("cell 追加 ボタン")
263
- ```
265
+ ```
266
+
267
+ **試したこと**
268
+ ```
269
+ if cell?.textLabel?.text != "cell 追加 ボタン" && cell.textLabel?.text != "textField.text!" {
270
+
271
+ ```
272
+
273
+ @IBAction func addbtr
274
+ のなかのself.kei.append(textField.text!) をcell.textLabel?.text != "title"に当てはめればおそらく私のやりたい事が出来ると思うんですけどtextField.text!の場合""に入れても"cell 追加 ボタン"のようにチェックつかないようにならないのです。

3

コードの追加

2017/10/04 02:15

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -254,4 +254,10 @@
254
254
 
255
255
  ```
256
256
 
257
- ![イメージ説明](7030344f5bb0f7a975ef270b18ac45af.png)
257
+ ![イメージ説明](7030344f5bb0f7a975ef270b18ac45af.png)
258
+
259
+ ```
260
+ @IBAction func addbtr(_ sender: Any) {
261
+    self.kei.append(textField.text!)
262
+ self.kei.append("cell 追加 ボタン")
263
+ ```

2

画像とコードの変更

2017/10/02 06:52

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -1,6 +1,89 @@
1
1
  ```
2
+ import UIKit
3
+
2
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
4
+ class ViewController: UIViewController, UITableViewDelegate,UITableViewDataSource {
5
+
6
+ @IBOutlet weak var mytableView: UITableView!
7
+
8
+ var kei = [String]()//["cell 追加 ボタン"]
9
+
10
+ override func viewDidLoad() {
11
+ super.viewDidLoad()
12
+
13
+ mytableView.delegate = self
14
+ mytableView.dataSource = self
15
+ // trueで複数選択、falseで単一選択
16
+
17
+ }
18
+
3
19
 
20
+ @IBAction func addbtr(_ sender: Any) {
21
+
22
+
23
+ let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert)
24
+
25
+ // OKボタンの設定
26
+ let okAction = UIAlertAction(title: "OK", style: .default, handler: {
27
+ (action:UIAlertAction!) -> Void in
28
+
29
+ // OKを押した時入力されていたテキストを表示
30
+ if let textFields = alert.textFields {
31
+
32
+ // アラートに含まれるすべてのテキストフィールドを調べる
33
+ for textField in textFields {
34
+ self.kei.append(textField.text!) //insertは上に追加されappendは下に追加される
35
+ self.kei.append("cell 追加 ボタン")
36
+ }
37
+ self.mytableView.reloadData()
38
+
39
+ }
40
+ })
41
+ alert.addAction(okAction)
42
+
43
+ // キャンセルボタンの設定
44
+ let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
45
+ alert.addAction(cancelAction)
46
+
47
+ // テキストフィールドを追加
48
+ alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in
49
+ textField.placeholder = "テキスト"
50
+ })
51
+
52
+
53
+ alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生
54
+
55
+ // アラートを画面に表示
56
+ self.present(alert, animated: true, completion: nil)
57
+
58
+ }
59
+
60
+ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
61
+ return kei.count
62
+ }
63
+
64
+
65
+ func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
66
+
67
+ switch indexPath.row {
68
+ // 選択不可にしたい場合は"nil"を返す
69
+ case 1:
70
+ return indexPath;
71
+
72
+ // 選択可にしたい場合は"indexPath"を返す
73
+ default:
74
+ return nil;
75
+ }
76
+ }
77
+
78
+ func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
79
+ if kei[indexPath.row] != "cell 追加 ボタン" {
80
+ return nil
81
+ }
82
+ return indexPath
83
+ }
84
+
85
+ func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
86
+
4
87
  let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
5
88
  //追加した内容に対して
6
89
  cell.textLabel?.text = kei[indexPath.row]
@@ -11,7 +94,6 @@
11
94
  if kei[indexPath.row] != "cell 追加 ボタン" {
12
95
  cell.textLabel?.textColor = UIColor.black
13
96
  cell.backgroundColor = UIColor.yellow
14
-
15
97
  }
16
98
  if indexPath.row == 0 {
17
99
  cell.textLabel?.textColor = UIColor.black
@@ -26,13 +108,9 @@
26
108
 
27
109
  let cell = tableView.cellForRow(at: indexPath)
28
110
 
29
- if cell!.accessoryType == .checkmark{
30
- cell!.accessoryType = .none
31
- }else{
32
- cell!.accessoryType = .checkmark
33
- }
34
-
35
- if kei[indexPath.row] != "cell 追加 ボタン" {
111
+ if cell?.textLabel?.text != "cell 追加 ボタン" {
112
+ cell?.accessoryType = cell?.accessoryType == .checkmark ? .none : .checkmark
113
+ tableView.deselectRow(at: indexPath, animated: true)
36
114
  return nil
37
115
  }else {
38
116
  let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert)
@@ -74,6 +152,82 @@
74
152
  }
75
153
 
76
154
  }
155
+
156
+
157
+ //cellの削除について
158
+ func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
159
+ if editingStyle == .delete {
160
+ self.kei.remove(at: indexPath.row)
161
+ tableView.deleteRows(at: [indexPath], with: .fade)
162
+ }
163
+ }
164
+
165
+ func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
166
+
167
+ let delete = UITableViewRowAction(style: .normal, title: "削除") {ation, index in
168
+ self.kei.remove(at: (indexPath.row))
169
+ tableView.deleteRows(at: [indexPath], with: .fade)
170
+ }
171
+ delete.backgroundColor = .red
172
+
173
+ let edit = UITableViewRowAction(style: .normal, title: "編集") { action, index in
174
+
175
+ let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert)
176
+
177
+ // OKボタンの設定
178
+ let okAction = UIAlertAction(title: "OK", style: .default, handler: {
179
+ (action:UIAlertAction!) -> Void in
180
+
181
+ // OKを押した時入力されていたテキストを表示
182
+ if let textFields = alert.textFields {
183
+
184
+ // アラートに含まれるすべてのテキストフィールドを調べる
185
+ for textField in textFields {
186
+ self.kei[indexPath.row] = textField.text!
187
+ //self.kei.append(textField.text!)
188
+ }
189
+ self.mytableView.reloadData()
190
+
191
+ }
192
+ })
193
+ alert.addAction(okAction)
194
+
195
+ // キャンセルボタンの設定
196
+ let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
197
+ alert.addAction(cancelAction)
198
+
199
+ // テキストフィールドを追加
200
+ alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in
201
+ textField.placeholder = "テキスト"
202
+ })
203
+
204
+
205
+ alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生
206
+
207
+ // アラートを画面に表示
208
+ self.present(alert, animated: true, completion: nil)
209
+ print("edit")
210
+ }
211
+ edit.backgroundColor = .orange
212
+
213
+ return [delete,edit]
214
+ }
215
+
216
+ // trueを返すことでCellのアクションを許可しています
217
+ func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
218
+
219
+ if kei.count > (indexPath.row + 1) {
220
+ return true
221
+ }else{
222
+ return false
223
+ }
224
+ }
225
+
226
+ override func didReceiveMemoryWarning() {
227
+ super.didReceiveMemoryWarning()
228
+ // Dispose of any resources that can be recreated.
229
+ }
230
+ }
77
231
  ```
78
232
 
79
233
  **やりたい事**
@@ -98,4 +252,6 @@
98
252
  cell!.accessoryType = .checkmark
99
253
  }
100
254
 
101
- ```
255
+ ```
256
+
257
+ ![イメージ説明](7030344f5bb0f7a975ef270b18ac45af.png)

1

コードの追加

2017/10/02 06:49

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -11,20 +11,69 @@
11
11
  if kei[indexPath.row] != "cell 追加 ボタン" {
12
12
  cell.textLabel?.textColor = UIColor.black
13
13
  cell.backgroundColor = UIColor.yellow
14
+
14
15
  }
15
- if indexPath.row == 0 { //self.kei.append[String]() {
16
+ if indexPath.row == 0 {
16
17
  cell.textLabel?.textColor = UIColor.black
17
18
  cell.backgroundColor = UIColor.white
18
19
  }
19
20
 
21
+ return cell
22
+ }
23
+
24
+ /*if kei.count > (indexPath.row + 1)使うには func tableView(_ tableView: UITableView, didSelectRowAtIndexPath: IndexPath){ を func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) -> IndexPath? {*/
25
+ func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) -> IndexPath? {
26
+
27
+ let cell = tableView.cellForRow(at: indexPath)
20
28
 
21
- if kei[indexPath.row] != "cell 追加 ボタン"{
22
- cell.accessoryType = .checkmark
29
+ if cell!.accessoryType == .checkmark{
30
+ cell!.accessoryType = .none
23
31
  }else{
24
- cell.accessoryType = .none
32
+ cell!.accessoryType = .checkmark
25
33
  }
34
+
35
+ if kei[indexPath.row] != "cell 追加 ボタン" {
26
- return cell
36
+ return nil
37
+ }else {
38
+ let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert)
39
+
40
+ // OKボタンの設定
41
+ let okAction = UIAlertAction(title: "OK", style: .default, handler: {
42
+ (action:UIAlertAction!) -> Void in
43
+
44
+ // OKを押した時入力されていたテキストを表示
45
+ if let textFields = alert.textFields {
46
+
47
+ // アラートに含まれるすべてのテキストフィールドを調べる
48
+ for textField in textFields {
49
+ self.kei.insert(textField.text!, at: indexPath.row)//indexPath.rowで1,2,3,4のような順になる
50
+ //self.kei.insert(textField.text!, at:self.kei.count - 1)
27
- }
51
+ }
52
+ self.mytableView.reloadData()
53
+
54
+ }
55
+ })
56
+ alert.addAction(okAction)
57
+
58
+ // キャンセルボタンの設定
59
+ let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
60
+ alert.addAction(cancelAction)
61
+
62
+ // テキストフィールドを追加
63
+ alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in
64
+ textField.placeholder = "テキスト"
65
+ })
66
+
67
+
68
+ alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生
69
+
70
+ // アラートを画面に表示
71
+ self.present(alert, animated: true, completion: nil)
72
+
73
+ return indexPath
74
+ }
75
+
76
+ }
28
77
  ```
29
78
 
30
79
  **やりたい事**
@@ -39,16 +88,14 @@
39
88
  elseで消す処理は書いているのですが反応してくれません。
40
89
  それで下記のようにしてみればと考えたのですがcell追加時には確かにcellが追加されなくはなったのですがそしたら今度はcellにチェックマークがつかなくなりました
41
90
 
91
+ **func didSelectRowAtIndexPathでこのようなコードを追加したところ[cell 追加 ボタン]だけcellが追加出来るようになりました**
42
92
  ```
43
- if kei[indexPath.row] != "cell 追加 ボタン"{
93
+ let cell = tableView.cellForRow(at: indexPath)
94
+
95
+ if cell!.accessoryType == .checkmark{
44
- cell.accessoryType = .none
96
+ cell!.accessoryType = .none
45
- }else if kei.count > (indexPath.row + 1){
46
- cell.accessoryType = .checkmark
47
97
  }else{
48
- cell.accessoryType = .none
98
+ cell!.accessoryType = .checkmark
49
99
  }
50
- return cell
51
100
 
52
- ```
101
+ ```
53
-
54
- ![イメージ説明](4acc42ff41c20630da7665d53632d4a9.png)