ios - 更改表格 View 单元格行中的按钮颜色

标签 ios swift uitableview

我在表格 View 单元格行中有一些按钮

enter image description here

当我单击按钮时,我希望按钮将颜色更改为黄色而不影响按钮的另一行。 enter image description here 。例如,当我单击第 0 行按钮时,它将变为黄色,如果我单击第 1 行按钮,它也会变为黄色,但第 0 行和其他行按钮将变回/保持原始颜色。

最佳答案

根据您的情况,您可以覆盖 isHighlightedisSelected 属性。

示例(Swift 3)如下。

class MyCustomCell: UITableViewCell {

        @IBOutlet var weak aButton: UIButton!
        @IBOutlet var weak aLabel: UILabel!

        override var isSelected: Bool {
        didSet {
            // custom selected behavior
            aButton.isSelected = isSelected
            aLabel.textColor = (isSelected ? UIColor.red : UIColor.blue)
           }
        }

        override var isHighlighted: Bool {
            didSet {
               // custom highlighted behavior
               aButton.isSelected = isHighlighted
               aLabel.textColor = (isHighlighted ? UIColor.red : UIColor.blue)
            }
        }
}

关于ios - 更改表格 View 单元格行中的按钮颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49189945/

相关文章:

ios - 在 UITableViewCell 中更改大小类时 UIStackView 布局问题

ios - 如何固定tableFooterView的位置

ios - 关于 Restcomm SDK for iOS 的一般问题

objective-c - 是否有指南或备忘单可供我引用 Objective C 的所有语法?

ios - UITableViewCell initWithStyle :UITableViewCellStyleSubtitle is not working

ios - swift 4 : Adding dictionaries to Plist

ios - TableView 单元格中的 UILabel 和 UISwitch

ios - 从objective-c向php发送空参数

iphone - 如何知道 UIkeyboard 是否出现在 iOS 中?

ios - 用户界面文本字段 : Bottom Border