お気に入りマークを実装したのですが、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 } }
確証ないのでこちらに。cellのリユーズ仕様の仕業な気がします。cell作成時の初期化処理に留意してみてください。
回答1件
あなたの回答
tips
プレビュー