iOS swift UIImageView changer l'image dans la cellule tableView

J'ai un problème étrange dans la cell tableView Custom. pour comme l'action Image, j'écris ces codes dans la cell personnalisée appelée FeedViewCell :

 self.like.isUserInteractionEnabled = true let CommenttapGestureRecognizer = UITapGestureRecognizer(target:self, action:#selector(likehandleTap)) self.like.addGestureRecognizer(CommenttapGestureRecognizer) func likehandleTap(_ sender: UITapGestureRecognizer) { if self.like.image == UIImage(named: "like-btn-inactive") { self.like.image = UIImage(named: "like-btn-active") } else { self.like.image = UIImage(named: "like-btn-inactive") } } 

et TableViewController:

 func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "FeedCell", for: indexPath) as! FeedViewCell return cell } 

mais comme vous le voyez dans cette video lorsque je touche le button J'aime dans l'index 0 et que je change l'image, le button J'aime dans l'index 3 change aussi d'image. Pouvez-vous me dire les gars mon erreur s'il vous plaît?

Merci

Essayez de code son fonctionnement 100%

  var selectindex : Int? var selectedindex : NSMutableArray = NSMutableArray() @IBOutlet var tableview: UITableView! func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("LikeCell", forIndexPath: indexPath) let like: UIButton = (cell.viewWithTag(2) as! UIButton) let comment: UIButton = (cell.viewWithTag(3) as! UIButton) if selectedindex.containsObject(indexPath.row) { like.setBackgroundImage(UIImage.init(named: "like.png"), forState: .Normal) }else{ like.setBackgroundImage(UIImage.init(named: "like (1).png"), forState: .Normal) } comment.setBackgroundImage(UIImage(named: "chat.png"), forState: UIControlState.Normal) like.addTarget(self, action: #selector(self.CloseMethod(_:event:)), forControlEvents: .TouchDown) comment.addTarget(self, action: #selector(self.CloseMethod1(_:event:)), forControlEvents: .TouchDown) return cell } @IBAction func CloseMethod(sender: UIButton, event: AnyObject) { let touches = event.allTouches()! let touch = touches.first! let currentTouchPosition = touch.locationInView(self.tableview) let indexPath = self.tableview.indexPathForRowAtPoint(currentTouchPosition)! selectindex = indexPath.row if selectedindex.containsObject(selectindex!) { selectedindex.removeObject(selectindex!) }else{ selectedindex.addObject(selectindex!) } self.tableview.reloadData() } 

C'est pourquoi vous modifiez l'image chargée dans l' imageView puis avec la méthode tableView.dequeueReusableCell vous réutilisez cette cellule avec l'image modifiée.

Dans iOS, UITableView et UICollectionView appliquent le concept de cellules réutilisables . Ils ne créent pas une cellule pour chaque élément du tableau; ils créent juste 3-4 cellules et ensuite ils vont le réutiliser juste en changeant le contenu à l'intérieur. C'est une bonne chose car cela permet aux développeurs de créer des tables avec des centaines de lignes sans avoir de problèmes de memory.

Ce sont les étapes suivies par la tableView (et aussi collectionView ) pour montrer le tableau d'éléments:

  1. prepareForReuse
  2. cellForRowAtIndexPath
  3. willDisplayCell

Pour résoudre votre problème, il vous suffit de cocher la cellForRowAtIndexPath dans cellForRowAtIndexPath

Exemple:

 func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "FeedCell", for: indexPath) as! FeedViewCell let model = arrayOfElements[indexPath.row] if model.isLiked { cell.like.image == UIImage(named: "like-btn-active") } else { cell.like.image == UIImage(named: "like-btn-active") } return cell } 

Prendre un tableau

 let arr : NSMutableArray = NSMutableArray() 

Et dans tableview, prenez le button au lieu de l'image et réglez contrairement à l'image dans le button et définissez vos images à la place de mes images

  func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 10 } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) let btn = cell.viewWithTag(10) as! UIButton btn.addTarget(self, action: #selector(ViewController.connected(_:)), forControlEvents: .TouchUpInside) return cell } func connected(sender: UIButton){ let buttonposition = sender.convertPoint(CGPointZero, toView: self.tblview) let index = self.tblview.indexPathForRowAtPoint(buttonposition) if !arr.containsObject((index?.row)!) { arr.addObject((index?.row)!) sender.setImage(UIImage(named: "ic_check.png"), forState: .Normal) }else { arr.removeObject((index?.row)!) sender.setImage(UIImage(named: "ic_uncheck.png"), forState: .Normal) } }