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

質問編集履歴

2

コードの追加と画像の追加

2017/10/20 01:27

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -260,6 +260,14 @@
260
260
  **試したこと**
261
261
  https://teratail.com/questions/94578を参考にし、色々試した中で構造体を使ってみたのですがcellの追加時にやっぱりにチェックマークの位置がずれて困っています。
262
262
 
263
+ ![イメージ説明](8b496acd3e572b6d49e1b80617fa2787.png)
263
264
 
265
+
264
266
  **//コード変更後**
265
- cellforRowAtにチェックマークの処理を書くとタップしてチェックマークつけたいのにタップしても反応しなくなります。
267
+ cellforRowAtにチェックマークの処理を書くとタップしてチェックマークつけたいのにタップしても反応しなくなります。
268
+
269
+ ```ここに言語を入力
270
+ tableView.reloadRows(at: [indexPath], with: .none)
271
+ ```
272
+
273
+ こうすることによってタップしてチェックマークをつけれるようになったのですが、なぜかcellを追加すると[cell 追加 ボタン]だけ勝手にチェックがついてしまうのですがなぜなのでしょうか?

1

コードの修正

2017/10/20 01:27

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -82,13 +82,13 @@
82
82
 
83
83
  let item = kei[indexPath.row]
84
84
  cell.textLabel?.text = item.name
85
- //cell.textLabel?.text = kei[indexPath.row]
86
85
  cell.textLabel?.textColor = UIColor.red
87
86
  cell.backgroundColor = UIColor.black
88
87
 
89
88
 
90
89
 
91
90
  if cell.textLabel?.text != "cell 追加 ボタン" {
91
+      cell.accessoryType = item.complete ? .checkmark : .none
92
92
  cell.textLabel?.textColor = UIColor.black
93
93
  cell.backgroundColor = UIColor.yellow
94
94
 
@@ -101,13 +101,12 @@
101
101
  return cell
102
102
  }
103
103
 
104
- /*if kei.count > (indexPath.row + 1)使うには func tableView(_ tableView: UITableView, didSelectRowAtIndexPath: IndexPath){ を func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) -> IndexPath? {*/
105
104
  func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) -> IndexPath? {
106
105
 
107
106
  let cell = tableView.cellForRow(at: indexPath)
108
107
 
109
108
  if cell?.textLabel?.text != "cell 追加 ボタン"{
110
- cell?.accessoryType = cell?.accessoryType == .checkmark ? .none : .checkmark
109
+ kei[indexPath.row].toggle()
111
110
  tableView.deselectRow(at: indexPath, animated: true)
112
111
  return nil
113
112
  }else {
@@ -259,4 +258,8 @@
259
258
  cellの追加時にチェックマークがずれないようにしたいです
260
259
 
261
260
  **試したこと**
262
- https://teratail.com/questions/94578を参考にし、色々試した中で構造体を使ってみたのですがcellの追加時にやっぱりにチェックマークの位置がずれて困っています。
261
+ https://teratail.com/questions/94578を参考にし、色々試した中で構造体を使ってみたのですがcellの追加時にやっぱりにチェックマークの位置がずれて困っています。
262
+
263
+
264
+ **//コード変更後**
265
+ cellforRowAtにチェックマークの処理を書くとタップしてチェックマークつけたいのにタップしても反応しなくなります。