【问题标题】:Table View Cell Button, Change Button Image once Clicked表格视图单元格按钮,单击后更改按钮图像
【发布时间】:2018-07-06 13:51:50
【问题描述】:

目前我的项目有一个表格视图控制器,每行都有一个按钮和文本。单击按钮后,我无法弄清楚如何更改特定行上的按钮图像。我已经有函数“覆盖 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)”可以将我的应用程序带到一个新视图。我需要按钮(单击时)将特定行上的数据添加到收藏夹变量。然后将显示在不同的屏幕上。我的按钮不会改变视图。

目前我有一个按钮事件函数,每次单击一行中的按钮时都会调用该函数。我遇到的问题是我不知道如何访问被单击的行中的特定按钮,并且只更改该按钮的图像。

这是我的代码:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell

    // Setting Text and images
    cell.batchName.text = processes[indexPath.item].batch_name
    cell.startTime.text = processes[indexPath.item].starttime
    cell.endTime.text = processes[indexPath.item].endtime
    cell.slaTime.text = processes[indexPath.item].SLA
    var statusColor = UIColor.black

    // Calling Button event function
    cell.starButton.addTarget(self, action: #selector(favoriteStarClicked), for: UIControlEvents.touchUpInside)

    return cell
}

@IBAction func favoriteStarClicked(_ sender: Any) {

    // Need to change the image of the button that was clicked 
}

【问题讨论】:

  • 为什么不子类化按钮并覆盖setSelected 方法。您不需要像这样跟踪按钮。
  • 你为什么不继承 UITableViewCell 并在那里拥有所有逻辑?
  • 我已经有函数“覆盖 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)”将我的应用程序带到一个新视图。我需要按钮(单击时)将特定行上的数据添加到收藏夹变量。然后将显示在不同的屏幕上。我的按钮不会改变视图。
  • @Brandi 你在原帖里问的和你现在说的完全不同。
  • @Desdenova 我将相应地编辑我的原始帖子。抱歉,这是我第一次提问。

标签: ios swift uitableview uibutton


【解决方案1】:

现代的 Swift 方式是回调闭包

  • 在模型中添加收藏状态的属性

    var isFavorite = false
    
  • 在 Interface Builder 中选择自定义单元格中的按钮,然后按 ⌥⌘4 转到 Attributes Inspector。在弹出菜单State Config 中选择Selected,然后在Image 弹出菜单中选择star 图像(将状态Default 的图像留空)。

  • 在自定义单元格中添加callback 属性和按钮的操作(将其连接到按钮)。图片是通过按钮的isSelected属性设置的

    var callback : (()->())?
    
    @IBAction func push(_ sender: UIButton) {
        callback?()
    }
    
  • cellForRow中根据isFavorite设置图片并添加回调

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
    
        // Setting Text and images
        cell.batchName.text = processes[indexPath.item].batch_name
        cell.startTime.text = processes[indexPath.item].starttime
        cell.endTime.text = processes[indexPath.item].endtime
        cell.slaTime.text = processes[indexPath.item].SLA
        var statusColor = UIColor.black
        let item = processes[indexPath.row]
        cell.button.isSelected = item.isFavorite
        cell.callback = {
            item.isFavorite = !item.isFavorite
            cell.button.isSelected = item.isFavorite
        }
    
        return cell
    }
    

回调更新模型和单元格中的图像


  • 没有协议/委托。
  • 没有自定义目标/操作。
  • 没有标签。
  • 没有索引路径。
  • 没有单元格框架数学。

【讨论】:

  • 有趣的回调实现!如果我的按钮是以编程方式创建的,您能否说一下如何更改图像?我试过了,但它没有改变按钮的状态: cell.addToFavorites.isSelected = movie.isFavorite cell.favoritesCallback = { movie.isFavorite = !movie.isFavorite cell.addToFavorites.isSelected = ((movie.isFavorite ? UIImage( systemName: "heart") : UIImage(systemName: "heart.fill")) != nil)
  • 你能给点小费吗?
  • 对不起,我不知道。请提出问题并提供更多代码和其他信息。
猜你喜欢
  • 2018-04-19
  • 2014-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多