重复使用单元格不行 – TableView

我有一个关于我的单元格button的问题。 在我的tableView中,每一行都由一个图像,一些标签和一个button组成。 该button有一个复选标记图像。 点击时,button的图像会改变。 问题是另一个button的图像没有理由变化。 发生这个错误是因为我的单元格被重用。

我试图在TableViewCell中使用prepareForReuse方法,但没有任何反应。 我也尝试了与selectedRowAt但我没有任何结果。 请帮帮我。

图片1:

图片1

图2:

图片2

这是我的custom Cell: func custom Cell:

  override func prepareForReuse() { if checkStr == "uncheck"{ self.checkBook.setImage(uncheck, for: .normal) } else if checkStr == "check"{ self.checkBook.setImage(check, for: .normal) } } func isPressed(){ let uncheck = UIImage(named:"uncheck") let check = UIImage(named: "check") if self.checkBook.currentImage == uncheck{ checkStr == "check" } else self.checkBook.currentImage == check{ checkStr == "uncheck" } } 

在我的tableView

  override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let selectedCell: ListPropertyUserCell = tableView.cellForRow(at: indexPath) as! ListPropertyUserCell let uncheck = UIImage(named:"uncheck") let check = UIImage(named: "check") if selectedCell.checkBook.imageView?.image == uncheck{ selectedCell.checkStr = "check" } else if selectedCell.checkBook.imageView?.image == check{ selectedCell.checkStr = "uncheck" } } 

从您的文章中的信息,这看起来像一个单元格重用问题。 问题是tableView重用了单元格而不是创build新单元格来维护性能。 如果你没有重置单元格的状态,重用的单元格将保持旧状态。

为了快速修复,你可以在UITableViewCell上实现prepareForReuse方法。

但是,如果您希望在滚动tableView之后选中checkbox,则需要在视图控制器中存储“已检查”哪个单元格。 你可以自己保存,也可以使用tableView的didSelectRowAtIndexPath方法。

试着这样做:

 var checkBook = UIImageView() if self.checkBook.image == UIImage(named: "check"){ self.checkBook.image = UIImage(named: "uncheck") } else{ self.checkBook.image = UIImage(named: "check") } 

如果您使用的是整个单元格的单击,则可以像这样覆盖自定义单元格中的setSelected func。

 override func setSelected(selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) if selected { self.checkBook.image = UIImage(named: "check") } else { self.checkBook.image = UIImage(named: "uncheck") } } 

UITableViewCell是可重用的。 您不能在单元格中存储查看状态。 你应该设置单元格

 func tableView(UITableView, cellForRowAt: IndexPath) 

你的数据源的方法

最简单的方法是实现

 func tableView(UITableView, didSelectRowAt: IndexPath) func tableView(UITableView, didDeselectRowAt: IndexPath) 

UITableViewDelegate的方法

然后,您可以在这些方法和cellForRowAtIndexPath安装单元格中将indexPath添加到/删除某些数组。

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("YourTableViewCell") as! YourTableViewCell if array.contains(indexPath) { cell.checkBook.image = UIImage(named: "check") } else { cell.checkBook.image = UIImage(named: "uncheck") } return cell } 

试试我的代码。 这里selectindex是用来获取选定的单元格索引和selectedindex是NSMutableArray,我存储所有选定的单元格值。

 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)// like button let comment: UIButton = (cell.viewWithTag(3) as! UIButton) // comment button comment.setBackgroundImage(UIImage(named: "chat.png"), forState: UIControlState.Normal) // comment button set like.addTarget(self, action: #selector(self.CloseMethod(_:event:)), forControlEvents: .TouchDown) comment.addTarget(self, action: #selector(self.CloseMethod1(_:event:)), forControlEvents: .TouchDown) return cell } // This is my like button action method. @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!) { sender.setBackgroundImage(UIImage.init(named: "like (1).png"), forState: .Normal) selectedindex.removeObject(selectindex!) }else{ sender.setBackgroundImage(UIImage.init(named: "like.png"), forState: .Normal) selectedindex.addObject(selectindex!) } }