回答編集履歴

1

didSet

2018/11/19 01:21

投稿

fuzzball
fuzzball

スコア16731

test CHANGED
@@ -1 +1,119 @@
1
1
  表示ONのアイテムを抽出して、それをTableViewに与えて下さい。
2
+
3
+
4
+
5
+ # コード追記
6
+
7
+
8
+
9
+ ```swift
10
+
11
+ var joinedItems = [NSDictionary]() //参加済アイテム保存用
12
+
13
+ var items = [NSDictionary]() {
14
+
15
+ didSet {
16
+
17
+ //itemsn更新時にjoinedItemsを生成し直す
18
+
19
+ joinedItems = items.filter {$0["PointPaid"] as? Int != nil}
20
+
21
+ }
22
+
23
+ }
24
+
25
+ ```
26
+
27
+
28
+
29
+ という仕組みで、自動的に表示ONのアイテムを抽出できるので、これをTableViewに与えてください。
30
+
31
+
32
+
33
+ * セル数取得 `items.count` → `joinedItems`
34
+
35
+ * セル生成時 `items[indexPath.row]` → `joinedItems[indexPath.row]`
36
+
37
+
38
+
39
+ ## 動作確認
40
+
41
+
42
+
43
+ ```swift
44
+
45
+ items += [
46
+
47
+ ["id": 0, "PointPaid": 123],
48
+
49
+ ["id": 1],
50
+
51
+ ["id": 2, "PointPaid": "abc"],
52
+
53
+ ]
54
+
55
+ print(joinedItems)
56
+
57
+ /*
58
+
59
+ [{
60
+
61
+ PointPaid = 123;
62
+
63
+ id = 0;
64
+
65
+ }]
66
+
67
+ */
68
+
69
+
70
+
71
+ items.append(["id": 3, "PointPaid": 999])
72
+
73
+ print(joinedItems)
74
+
75
+ /*
76
+
77
+ [{
78
+
79
+ PointPaid = 123;
80
+
81
+ id = 0;
82
+
83
+ }, {
84
+
85
+ PointPaid = 999;
86
+
87
+ id = 3;
88
+
89
+ }]
90
+
91
+ */
92
+
93
+
94
+
95
+ items.remove(at: 0) //id0削除
96
+
97
+ print(joinedItems)
98
+
99
+ /*
100
+
101
+ [{
102
+
103
+ PointPaid = 999;
104
+
105
+ id = 3;
106
+
107
+ }]
108
+
109
+ */
110
+
111
+ ```
112
+
113
+
114
+
115
+ # ひとりごと
116
+
117
+
118
+
119
+ 参考にした記事が古いんだと思いますが、NSDictionaryではなくDictionaryの方がいいし、いちいちIndexPathをNSIndexPathにキャストしなくていいです。