【问题标题】:Change rows button image source on click?单击时更改行按钮图像源?
【发布时间】:2015-10-29 10:07:19
【问题描述】:

我正在尝试实现代码以在单击该行时更改该行的按钮图像源。这是一个清单表视图。表视图分为多个部分。不知道我做错了什么:

 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
    }

【问题讨论】:

  • @DharmeshKheni 是的,就在具有所有 tableview 方法的视图控制器中
  • 没有 alpha 值,你的 didselect 工作正常吗?
  • @Vijay 好的,所以当我点击一行时,我会按顺序获取 didDeSelect 和 didSelect 的日志。然后,如果我第二次、第三次、第四次点击该行,我只会得到 didSelect
  • 所以我觉得你不能用这种方法实现你想要的。您需要重新加载特定行以切换刻度图像
  • 你能在 indexpath 中显示你的单元格吗?

标签: ios swift uitableview sections


【解决方案1】:

试试这个:

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

编辑:在聊天中与 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
}

【讨论】:

  • 我刚刚在我的更新中使用了该方法,与我相比,以您的方式实现它有什么优势吗?
  • 我认为你的做法是正确的!很高兴知道它已经解决了!
  • 我的方法根本不起作用,当我上下滚动时,不同的行会随机改变 img src。我现在正在尝试你的方法。唯一的问题是如何定义 cellTickedImage?
  • 在班级级别定义cellTickedImage 。它应该是一个全局属性,可以从所有类函数中访问。
  • 当前类如下,那么cellTickedImage如何定义呢?没有出口? CheckListCell : UITableViewCell { IBOutlet 弱 var tickBtn: UIButton! IBOutlet 弱 var taskLbl:UILabel! }
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    试试:

    第 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:你可以做一些更好的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-15
      • 2013-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-14
      • 2017-01-21
      相关资源
      最近更新 更多