質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Objective-C

Objective-Cはオブジェクト指向型のプログラミング言語のひとつです。C言語をベースにSmalltalkが取り入れられています。

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Q&A

解決済

2回答

2738閲覧

TableViewを下にスクロールするとセルの表示がおかしくなる

tarofess

総合スコア127

Objective-C

Objective-Cはオブジェクト指向型のプログラミング言語のひとつです。C言語をベースにSmalltalkが取り入れられています。

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

0グッド

0クリップ

投稿2016/01/13 03:24

編集2016/01/13 04:42

現在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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

多分TableViewのセルの再利用に関する問題なのではないかと思うのですが、

その通りです。
CustomCellのセットアップをしている箇所で、paySwitchの状態も設定しないといけません。そうしないと、再利用前に使われていた状態を引き継いでしまいます。

objectivec

1ordersItemCell.paySwitch.on = YES or No; //Switchの状態をどこかに保存しておいてここで復元する

今回の場合だと、ONにした一番上のセルが画面から消えて、下の方のセルに再利用された時にONの状態で表示されているわけです。

投稿2016/01/13 04:05

fuzzball

総合スコア16731

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

tarofess

2016/01/13 04:44

ご回答ありがとうございます。スイッチの状態を保存するプロパティを作成してセルのスイッチを制御するコードを書いたのですが、うまくいきませんでした。。。詳しくは質問に追記しましたので、ご確認頂けたらと思います。すみませんが、宜しくお願いします。
fuzzball

2016/01/13 04:51

CustomCellに復元させる情報をCustomCell自身に保存しても意味がないでしょう。例えば、ordersArray にBOOL型のメンバを追加するとか、別にBOOL型の配列を用意するとか。
tarofess

2016/01/13 07:22

ordersArrayの個数分BOOL型の配列を用意してその配列にスイッチの状態を保存するようにすることでセルの表示がおかしくなることがなくなりました。ありがとうございました。
guest

0

if-elseの両ブロックにUiViewの値を設定する処理を記述するべきです。

投稿2016/01/13 03:36

yona

総合スコア18155

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

tarofess

2016/01/13 04:44

ご回答ありがとうございます。スイッチの状態を保存するプロパティを作成してセルのスイッチを制御するコードを書いたのですが、うまくいきませんでした。。。詳しくは質問に追記しましたので、ご確認頂けたらと思います。すみませんが、宜しくお願いします。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問