iOS swift UIImageView在tableView单元格中更改图像

我在tableView自定义cell有一个奇怪的问题。 对于像Image操作,我将这些代码写在Custom cell称为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") } } 

和TableViewController:

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

但正如你在这个video中看到的,当我触摸索引0中的类似button并改变图像时,索引3中的类似button也会改变图像。 你能告诉我我的错误吗?

谢谢

尝试代码工作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() } 

这就是为什么你要改变imageView加载的图像,然后用方法tableView.dequeueReusableCell你正在重新使用更改后的图像单元格。

在iOS中, UITableViewUICollectionView应用了可重用单元的概念。 他们不为数组的每个元素创build一个单元格; 他们只是创build3-4个单元格,然后他们将重新使用它只是改变内部的内容。 这是一件好事,因为它允许开发人员在不存在内存问题的情况下创build包含数百行的表格。

这些是tableView (也是collectionView )显示元素数组所遵循的步骤:

  1. prepareForReuse
  2. 的cellForRowAtIndexPath
  3. willDisplayCell

要解决你的问题,你只需要在cellForRowAtIndexPath检查

例:

 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 } 

拿一个数组

 let arr : NSMutableArray = NSMutableArray() 

并在桌面视图采取button,而不是图像和设置不像图像中的button,并设置您的图像,而不是我的图像

  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) } }