現在TableViewのセルにはスイッチが1つとラベルが2つ置かれています。
そして一番上のセルのスイッチをタップしてONにした後、TableViewを下の方にスクロールすると、なぜか下の方のセルのスイッチまでONになっています。多分TableViewのセルの再利用に関する問題なのではないかと思うのですが、どうすれば選択したセルのスイッチのみをONにすることができるでしょうか?
以下に現在のコードを記します。
どなたか解決できる方がいれば教えていただきたいです。
宜しくお願いします。
CustomCell.h
Objective
1@protocol PaySwitchDelegate <NSObject> 2 3- (void)addPayItem: (int)switchTag: (BOOL)isSwitchOn; 4 5@end 6 7@interface CustomCell : UITableViewCell 8 9@property (weak, nonatomic) IBOutlet UISwitch *paySwitch; 10@property (weak, nonatomic) IBOutlet UILabel *nameLabel; 11@property (weak, nonatomic) IBOutlet UILabel *price; 12 13@property id<PaySwitchDelegate> delegate; 14 15@end
ViewController.m
Objective
1- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 2 UITableViewCell *cell; 3 4 if (didTapSeparatePaymentBarButtonItem) { 5 cell = [self.tableView dequeueReusableCellWithIdentifier:@"CustomCell" forIndexPath:indexPath]; 6 CustomCell *ordersItemCell = (CustomCell*)cell; 7 8 ordersItemCell.nameLabel.text = ordersArray[indexPath.row].orderLinesName; 9 ordersItemCell.price.text = [NSString stringWithFormat:@"%d", ordersArray[indexPath.row].price]; 10 ordersItemCell.paySwitch.tag = indexPath.row; 11 ordersItemCell.delegate = self; 12 13 } else { 14 cell = [self.tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 15 16 cell.textLabel.text = ordersArray[indexPath.row].orderLinesName; 17 cell.detailTextLabel.text = [NSString stringWithFormat:@"%d", ordersArray[indexPath.row].price]; 18 } 19 20 return cell; 21} 22
////////////////////////回答を受けて追記///////////////////////////
回答を受けてスイッチの状態を保存するBOOL型のプロパティを作成し、そのプロパティがtrueだとスイッチをcellForRowAtIndexPath内でONにするというように実装したのですが、またもTableViewの下の方のスイッチがONになってしまいました。。。
新しく作成したコードのどこが間違っているのでしょうか?
CustomCell.h
Objective
1@protocol PaySwitchDelegate <NSObject> 2 3- (void)addPayItem: (int)switchTag: (BOOL)isSwitchOn; 4 5@end 6 7@interface CustomCell : UITableViewCell 8 9@property (weak, nonatomic) IBOutlet UISwitch *paySwitch; 10@property (weak, nonatomic) IBOutlet UILabel *nameLabel; 11@property (weak, nonatomic) IBOutlet UILabel *price; 12 13@property id<PaySwitchDelegate> delegate; 14@property BOOL didSwitchOn; //追記 15 16@end
CustomCell.m
Objective
1- (void)didTapPaySwitch: (UISwitch*)paySwitch { 2 if (paySwitch.on) { 3 [self.delegate addPayItem:paySwitch.tag :YES]; 4 5 } else { 6 [self.delegate addPayItem:paySwitch.tag :NO]; 7 } 8}
ViewController.m
Objective
1- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 2 UITableViewCell *cell; 3 4 if (didTapSeparatePaymentBarButtonItem) { 5 cell = [self.tableView dequeueReusableCellWithIdentifier:@"CustomCell" forIndexPath:indexPath]; 6 CustomCell *ordersItemCell = (CustomCell*)cell; 7 8 ordersItemCell.nameLabel.text = ordersArray[indexPath.row].orderLinesName; 9 ordersItemCell.price.text = [NSString stringWithFormat:@"%d", ordersArray[indexPath.row].price]; 10 ordersItemCell.paySwitch.tag = indexPath.row; 11 ordersItemCell.delegate = self; 12 13 if (ordersItemCell.didSwitchOn) { //このif-selseを追記 14 [ordersItemCell.paySwitch setOn:YES animated:YES]; 15 } else { 16 [ordersItemCell.paySwitch setOn:NO animated:YES]; 17 } 18 19 } else { 20 cell = [self.tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 21 22 cell.textLabel.text = ordersArray[indexPath.row].orderLinesName; 23 cell.detailTextLabel.text = [NSString stringWithFormat:@"%d", ordersArray[indexPath.row].price]; 24 } 25 26 return cell; 27} 28 29- (void)addPayItem:(int)switchTag :(BOOL)isSwitchOn { 30 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:switchTag inSection:0]; 31 IPOrdersItemCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; 32 cell.didSwitchOn = isSwitchOn; 33} 34 35
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/01/13 04:44
2016/01/13 04:51
2016/01/13 07:22