質問編集履歴

1

回答を受けて質問を追記

2016/01/13 04:42

投稿

tarofess
tarofess

スコア127

test CHANGED
File without changes
test CHANGED
@@ -101,3 +101,163 @@
101
101
 
102
102
 
103
103
  ```
104
+
105
+
106
+
107
+
108
+
109
+ ////////////////////////回答を受けて追記///////////////////////////
110
+
111
+
112
+
113
+ 回答を受けてスイッチの状態を保存するBOOL型のプロパティを作成し、そのプロパティがtrueだとスイッチをcellForRowAtIndexPath内でONにするというように実装したのですが、またもTableViewの下の方のスイッチがONになってしまいました。。。
114
+
115
+ 新しく作成したコードのどこが間違っているのでしょうか?
116
+
117
+
118
+
119
+ CustomCell.h
120
+
121
+
122
+
123
+ ```Objective-c
124
+
125
+ @protocol PaySwitchDelegate <NSObject>
126
+
127
+
128
+
129
+ - (void)addPayItem: (int)switchTag: (BOOL)isSwitchOn;
130
+
131
+
132
+
133
+ @end
134
+
135
+
136
+
137
+ @interface CustomCell : UITableViewCell
138
+
139
+
140
+
141
+ @property (weak, nonatomic) IBOutlet UISwitch *paySwitch;
142
+
143
+ @property (weak, nonatomic) IBOutlet UILabel *nameLabel;
144
+
145
+ @property (weak, nonatomic) IBOutlet UILabel *price;
146
+
147
+
148
+
149
+ @property id<PaySwitchDelegate> delegate;
150
+
151
+ @property BOOL didSwitchOn; //追記
152
+
153
+
154
+
155
+ @end
156
+
157
+ ```
158
+
159
+
160
+
161
+ CustomCell.m
162
+
163
+
164
+
165
+ ```Objective-c
166
+
167
+ - (void)didTapPaySwitch: (UISwitch*)paySwitch {
168
+
169
+ if (paySwitch.on) {
170
+
171
+ [self.delegate addPayItem:paySwitch.tag :YES];
172
+
173
+
174
+
175
+ } else {
176
+
177
+ [self.delegate addPayItem:paySwitch.tag :NO];
178
+
179
+ }
180
+
181
+ }
182
+
183
+ ```
184
+
185
+
186
+
187
+ ViewController.m
188
+
189
+
190
+
191
+ ```Objective-c
192
+
193
+ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
194
+
195
+ UITableViewCell *cell;
196
+
197
+
198
+
199
+ if (didTapSeparatePaymentBarButtonItem) {
200
+
201
+ cell = [self.tableView dequeueReusableCellWithIdentifier:@"CustomCell" forIndexPath:indexPath];
202
+
203
+ CustomCell *ordersItemCell = (CustomCell*)cell;
204
+
205
+
206
+
207
+ ordersItemCell.nameLabel.text = ordersArray[indexPath.row].orderLinesName;
208
+
209
+ ordersItemCell.price.text = [NSString stringWithFormat:@"%d", ordersArray[indexPath.row].price];
210
+
211
+ ordersItemCell.paySwitch.tag = indexPath.row;
212
+
213
+ ordersItemCell.delegate = self;
214
+
215
+
216
+
217
+ if (ordersItemCell.didSwitchOn) { //このif-selseを追記
218
+
219
+ [ordersItemCell.paySwitch setOn:YES animated:YES];
220
+
221
+ } else {
222
+
223
+ [ordersItemCell.paySwitch setOn:NO animated:YES];
224
+
225
+ }
226
+
227
+
228
+
229
+ } else {
230
+
231
+ cell = [self.tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
232
+
233
+
234
+
235
+ cell.textLabel.text = ordersArray[indexPath.row].orderLinesName;
236
+
237
+ cell.detailTextLabel.text = [NSString stringWithFormat:@"%d", ordersArray[indexPath.row].price];
238
+
239
+ }
240
+
241
+
242
+
243
+ return cell;
244
+
245
+ }
246
+
247
+
248
+
249
+ - (void)addPayItem:(int)switchTag :(BOOL)isSwitchOn {
250
+
251
+ NSIndexPath *indexPath = [NSIndexPath indexPathForRow:switchTag inSection:0];
252
+
253
+ IPOrdersItemCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
254
+
255
+ cell.didSwitchOn = isSwitchOn;
256
+
257
+ }
258
+
259
+
260
+
261
+
262
+
263
+ ```