单击更改行按钮图像源?

Change rows button image source on click?

我正在尝试实现通过单击行来更改行的按钮图像源的代码。这是一个清单 table view.The table 视图分为几个部分。不确定我做错了什么:

 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    //change tickBtn's image src to tick.png
    print("in didselect")
    if let cell = tableView.cellForRowAtIndexPath(indexPath) as? CheckListCell {
        let tickedImg = UIImage(named: "tick.png")
        cell.tickBtn.setImage(tickedImg, forState: UIControlState.Selected)
        //cell.tickBtn.alpha = 0

    }
    else{
        print("in the else of didSelect")
    }

}

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
    //change tickBtn's image src to unTicked.png
    print("in didDeselect")
    if let cell = tableView.cellForRowAtIndexPath(indexPath) as? CheckListCell{
        let tickedImg = UIImage(named: "unTicked.png")
        cell.tickBtn.setImage(tickedImg, forState: UIControlState.Selected)
        //cell.tickBtn.alpha = 1
    }
    else{
        print("in the else of didDeSelect")
    }

}

我正在获取 didSelect 和 didDeSelect 的日志,但图像源没有改变。一定与我访问单元格的方式有关,我需要考虑该部分吗?

更新:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    //change tickBtn's image src to tick.png
    let cell = tableView.cellForRowAtIndexPath(indexPath) as? CheckListCell
    cell!.cellTickedImage = UIImage(named: "tick.png")
    self.checkListTableView.beginUpdates()
    self.checkListTableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
    self.checkListTableView.endUpdates()

}

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
    let cell = tableView.cellForRowAtIndexPath(indexPath) as? CheckListCell
    cell!.cellTickedImage = UIImage(named: "unTicked.png")
    self.checkListTableView.beginUpdates()
    self.checkListTableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
    self.checkListTableView.endUpdates()
}

class CheckListCell : UITableViewCell {

    @IBOutlet weak var tickBtn: UIButton!
    @IBOutlet weak var taskLbl: UILabel!
    var cellTickedImage : UIImage?

}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = checkListTableView.dequeueReusableCellWithIdentifier("CheckListCell", forIndexPath: indexPath) as! CheckListCell

        cell.taskLbl?.text = checkListData[indexPath.section][indexPath.row]
        cell.tickBtn.setImage(cell.cellTickedImage, forState: UIControlState.Normal)
        cell.tickBtn.setImage(cell.cellTickedImage, forState: UIControlState.Highlighted)

        //cell.tickBtn.titleLabel!.text = ""

        return cell
    }

您是否也尝试将图像设置为正常状态?

因为我可以从您给定的代码中推断出,您只是在设置选定的状态图像。 并且按钮始终处于正常状态。 所以也尝试将图像设置为正常状态。

试试这个:

第 1 步:didSelectRowAtIndexPath 中更新驱动细胞图像的模型:

self.cellTickedImage = UIImage(named: "tick.png”)

第 2 步:didSelectRowAtIndexPath 中重新加载单元格以查看更改:

self.tableView.beginUpdates()
self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
self.tableView.endUpdates()

第 3 步:didDeselectRowAtIndexPath 中更新驱动细胞图像的模型:

self.cellTickedImage = UIImage(named: "unTicked.png”)

第 4 步:didDeselectRowAtIndexPath 中重新加载单元格以查看更改:

self.tableView.beginUpdates()
self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
self.tableView.endUpdates()

第 5 步:cellForRowAtIndexPath 中正确设置细胞图像:

cell.tickBtn.setImage(self.cellTickedImage, forState: UIControlState.Normal)
cell.tickBtn.setImage(self.cellTickedImage, forState: UIControlState.Highlighted)

编辑: Post 与 OP 的聊天讨论 - 讨论中的一些假设

  1. 两个单元格上的文本不得相同。
  2. 点击一次显示一张图片,点击第二次显示第二张图片。

考虑到这一点,这里是修复的高级算法:

  1. 创建一个全局字典 checkListDict,键作为单元格文本,值作为图像状态标志。所有单元格文本的初始设置值为 0。考虑 1 为 tick.png 和 0 为 unTicked.png.
  2. didSelectRowAtIndexPath 中像这样更新标志:

->

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
   //change tickBtn's image src to tick.png 
   let cell = tableView.cellForRowAtIndexPath(indexPath) as? CheckListCell 
   let selectedCellText = (cell!.taskLbl?.text)!

   if checkListDict[selectedCellText] == 0 { 
      checkListDict[selectedCellText] = 1 
   } 
   else{ 
      checkListDict[selectedCellText] = 0 
   } 

   self.checkListTableView.beginUpdates() 
   self.checkListTableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) 
   self.checkListTableView.endUpdates()
}
  1. 最后,像这样使用 cellForRowAtIndexPath 中的更新模型:

->

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = checkListTableView.dequeueReusableCellWithIdentifier("CheckListCell", forIndexPath: indexPath) as! CheckListCell

    let selectedCellText = checkListData[indexPath.section][indexPath.row]

    cell.taskLbl?.text = selectedCellText
    let cellImage = checkListDict[selectedCellText] == 0 ? UIImage(named: "unTicked.png")  : UIImage(named:"tick.png") 


    cell.tickBtn.setImage(cellImage, forState: UIControlState.Normal)
    cell.tickBtn.setImage(cellImage, forState: UIControlState.Highlighted)

    return cell
}

尝试:

第 1 步:使数组具有与您的行数相同的元素。并在 viewDidload 中设置:

for (i = 0; i < NumberElement; i++) {
    [self.listState addObject:@"0"];
}

第 2 步:在 cellForRowAtIndexPath 处检查它:

let tickedImg = UIImage(named: "unTicked.png")
    cell.tickBtn.setImage(tickedImg, forState: UIControlState.Selected
let untickedImg = UIImage(named: "ticked.png")
cell.tickBtn.setImage(untickedImg, forState: UIControlstate.Normal)
if self.listState[indexPath.row) == 0  {
   cell.tickBtn.selected = false
} else {
   cell.tickBtn.selected = true
}

第 3 步:在 didSelectRowAtIndexPath 中:

if self.listState[indexPath.row] == "0" {
  self.listState[indexPath.row] = "1"
} else {
  self.listState[indexPath.row] = "0"
}
//Start reload cell 
self.tableView.beginUpdates()
self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
self.tableView.endUpdates()

希望对您有所帮助。 P/s:你可以做一些更好的方法。