ios - 单击后无法设置 UITableViewCell 中的 UIButton 图像

标签 ios swift uitableview swift3 uibutton

UITableViewCell 中的按钮不响应其点击方法中的 setImage 方法。这是 TableView 的 cellForRowAt 部分。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell=tableView.dequeueReusableCell(withIdentifier: "BroadcastViewCell", for: indexPath) as!BroadcastViewCell
    var show=self.week?[selectedIndex].shows?[indexPath.item]
    cell.myLabel.text=show?.programName
    if let resim=show?.thumbURL{
        cell.myImage.downloadImage(from: resim)
    }
    cell.notifyButton.tag = indexPath.row
    cell.notifyButton.setImage( UIImage(named:"icnAlarm"), for: .normal)
    cell.notifyButton.setImage( UIImage(named:"icnAlarmCheck"), for: .selected)

    if (self.notifications?.contains(where: {$0.identifier == (show?.airDate)!+(show?.airTime)!}))!
    {
        cell.notifyButton.isSelected=true
    }else
    {
        cell.notifyButton.isSelected=false
    }
    cell.notifyButton.addTarget(self, action: #selector(buttonClicked(sender:)), for: .touchUpInside)
    return cell;
}

我只是在 buttonClicked 实现中设置它的选中状态。

func buttonClicked(sender:UIButton)
{
    sender.isSelected=true
}

但是图像在它之后没有改变。它仅在为其特定行调用 cellForRowAt 方法后发生变化。我不明白为什么它对同一代码部分的响应不同。感谢您的帮助。

最佳答案

你必须告诉按钮做什么:

func buttonClicked(sender:UIButton)
{
    if myButton.isSelected {
         myButton.isSelected = false
    }else{
       myButton.isSelected = true
    }
}

将您的按钮配置为自定义:

let myButton = UIButton(type: .custom)

或者在属性检查器中:

attributes Inspector forbutton

关于ios - 单击后无法设置 UITableViewCell 中的 UIButton 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45117964/

相关文章:

swift - AdMob 广告横幅不改变位置 (Swift 3)

iPhone/iPad SimpleTableViewCells : What replaces the deprecated setImage setText on table cells?

iphone - 离开页面后TableViewCells被删除

javascript - 如何让 HTML5 JavaScript Canvas 识别来自 iphone/ipad 的触摸/点击?

ios - 如何在巨大的 CollectionView 中使用 SDWebImage 或 AFNetworking 在磁盘上缓存图像?

ios - 像表格一样显示具有动态大小的标签,但不重叠

swift - 使用不同的数据库提供商测试 Vapor 3

ios - 如何根据按下的按钮更改 UITableView 中 Label 的文本?

iphone - 添加要存储的新对象时核心数据关系丢失

iphone - UITableView 带分页上一个,下一个按钮