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

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

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

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

2760閲覧

TableViewCell でお気に入りマークを実装したのですが、押していないお気に入りマークも周期性に従って押したことになってしまいます。

MasatoTunashima

総合スコア8

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

1クリップ

投稿2018/02/22 09:31

お気に入りマーク実装

お気に入りマークを実装したのですが、1つのボタンを押すと、押していないにも関わらずほかのボタンも押したことになってしまいます。
具体的にいうと、7の周期性で押したことになります。

(例)
1番目のセルの星を押す → 8番目や15番目のセルのお気に入れマークも押したことになってしまう。
1,2番目のセルの星を押す → 8,9番目や15,16番目のセルのお気に入りマークも押したことになってしまう。

7の周期で繰り返すように設定したつもりはないのですが、こちらデフォルトでそのような設定がなされているものなのでしょうか?
ご回答いただけますと、ありがたいです。

import UIKit import NCMB class ContainerBuyViewController: UIViewController,UITableViewDelegate,UITableViewDataSource { @IBOutlet weak var TableView: UITableView! //tableviewに出力するセルデータを保存するarray var itemArray : NSArray = NSArray() //mBaasからデータを取得する let query = NCMBQuery(className: "TestClass") override func viewDidLoad() { super.viewDidLoad() loadItemData() TableView.dataSource = self TableView.delegate = self as? UITableViewDelegate // Do any additional setup after loading the view. self.TableView.reloadData() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func loadItemData(){ let query: NCMBQuery = NCMBQuery(className: "itemClass") query.order(byDescending: "createData") query.findObjectsInBackground({(objects, error) in if error == nil{ if (objects?.count)! > 0{ self.itemArray = objects! as NSArray self.TableView.reloadData() } }else{ print(error!) } }) } func numberOfSections(in tableView: UITableView) -> Int { return 1 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return itemArray.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier:"BuyCell", for: indexPath) as! ContainerBuyTableViewCell let targetItemData: AnyObject = self.itemArray[indexPath.row] as AnyObject cell.CellTitle?.text = targetItemData.object(forKey: "item_title")as? String cell.CellSpeaker?.text = targetItemData.object(forKey: "item_price")as? String //cell.SpeakerImage = targetItemData.object(forKey: "thumbnail") as! UIImageView //favの動きをどのようにするか? return cell }
import UIKit class ContainerBuyTableViewCell: UITableViewCell { @IBOutlet weak var FavImage: UIButton! @IBOutlet weak var CellTitle: UILabel! @IBOutlet weak var CellSpeaker: UILabel! @IBOutlet weak var SpeakerImage: UIImageView! var count = 0 override func awakeFromNib() { super.awakeFromNib() // Initialization code } @IBAction func CellFav(_ sender: Any) { if (count % 2 == 0){ FavImage.setImage(#imageLiteral(resourceName: "Fav"), for: UIControlState()) count += 1 }else{ FavImage.setImage(#imageLiteral(resourceName: "NotFav"), for: UIControlState()) count += 1 } } override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state } }

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

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

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

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

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

fromageblanc

2018/02/22 10:21

確証ないのでこちらに。cellのリユーズ仕様の仕業な気がします。cell作成時の初期化処理に留意してみてください。
guest

回答1

0

ベストアンサー

コメントにて記載の通り、TableViewのCellは画面外に出ると再利用されるので7の周期で値が変更したように見えます。(つまり再利用されたセルが見えている)

countをcell側に持たせるのではなくdata側に持たせ

c tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

内でcount値をcellに代入し直せばいいとおもいます。

投稿2018/02/22 10:55

m0a

総合スコア708

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

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

MasatoTunashima

2018/03/01 09:11

ご回答頂きましてありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問