单击时在单元格中显示图像并在其他单元格中隐藏图像 Swift IOS

Show image in cell when click and hide image in other cell Swift IOS

首先,我将所有图像隐藏在单元格中。在我点击 did select 行后,该单元格将显示图像并再次单击它将隐藏图像。那个工作。但问题是当我点击那个单元格(不再点击)然后点击其他单元格时,(其他单元格将显示图像)但前一个单元格不会隐藏图像。

This code for cell and didselectrow

取一个包含当前节目图像的indexPath.row值的变量

 var showImageIndex : Int?

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{

   let tableCell = tableView.dequeueReusableCellWithIdentifier("cellName") as? CustomCell

   if showImageIndex == indexPath.row{
      tableCell.IBimageView.isHidden = false
   }else{
      tableCell.IBimageView.isHidden = true
   }
 }

 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
    showImageIndex = indexPath.row
    self.tableViewName.reload()
 }

在您的 didSelect 方法中,在对您的索引进行任何操作之前,您需要在旧单元格中隐藏图像。

这将在您的旧单元格中隐藏图片

if(index != nil){
    tableView.cellForRow(at:index!) as! FailOrderNoteTableViewCell
    cell.rightViewCell.hidden = true
}

代码中的其他问题是,如果您没有选择可以正常工作的单元格而不是第 99 行和第 99 节,则需要为索引设置 nil