简体   繁体   English

快速在表格视图上按下时如何更改标签文本的颜色?

[英]How to change a label text color when pressed on a table view with swift?

I have a UITableView with a cell that has 1 UILabel in it. 我有一个带有一个UILabel的单元格的UITableView I want the cell labels text color to change when the cell is pressed. 我希望单元格标签的文本颜色在单元格被按下时改变。 How do I do that? 我怎么做?

This is what I tried: 这是我尝试的:

 func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
        let cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell

        let label:UILabel = cell.viewWithTag(1) as UILabel
        label.textColor = UIColor(red: 46/255.0, green: 204/255.0, blue: 113/255.0, alpha: 1.0)
        cell.reloadInputViews()



    }

Subclass UITableViewCell and overwrite setSelected. 子类化UITableViewCell并覆盖setSelected。

@IBOutlet weak var label: UILabel!
override func setSelected(selected: Bool, animated: Bool) {
  if (selected) {
    label.textColor = UIColor.redColor()
  } else {
    label.textColor = UIColor.blackColor()
  }
}

Implement the didDeselectRowAtIndexPath delegate method. 实现didDeselectRowAtIndexPath委托方法。 In this method you should set the label back to the normal color. 在这种方法中,您应该将标签重新设置为正常颜色。

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
  let cell = tableView.cellForRowAtIndexPath(indexPath)
  let label: UILabel = cell.viewWithTag(1) as UILabel
  label.textColor = UIColor.redColor()
}

override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
  let cell = tableView.cellForRowAtIndexPath(indexPath)
  let label: UILabel = cell.viewWithTag(1) as UILabel
  label.textColor = UIColor.blackColor()
}

Also, remove .reloadInputViews() unless you actually need it. 此外,除非确实需要,否则请删除.reloadInputViews()。 This is probably wiping any updating of textColor. 这可能抹去了textColor的任何更新。

Swift 3 version looks like: Swift 3版本如下所示:

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
    if (selected) {
         self.artistLabel.textColor = UIColor.lightGray
    } else {
         self.artistLabel.textColor = UIColor.black
    }
}

just mention that this is much clear and better way to do it than in didSelectRowAtIndexPath 只需提一下,这是比didSelectRowAtIndexPath更清晰,更好的方法

What I generally uses for such situations is overriding setHighlighted in UITableViewCell subclass as: 我通常在这种情况下使用的是setHighlighted UITableViewCell子类中的setHighlighted重写为:

override func setHighlighted(_ highlighted: Bool, animated: Bool) {
    super.setHighlighted(highlighted, animated: animated)
    label.textColor = UIColor.redColor()
}

For this, you have to use custom table view cells to override particular method. 为此,您必须使用自定义表格视图单元格覆盖特定方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何更改表格视图单元格文本标签的颜色 - How to change table view cell text label color 更改标签文本时如何更改标签颜色 - How to change label color when you change label text 如何在按下特殊表格视图单元格时更改多个标签中的文本? - How to change the text in several labels when a special table view cell is pressed? 当在UIActivityViewController上单击“取消”按钮时,如何更改NavigationController的文本颜色? - How to change NavigationController text color when Cancel button is pressed on UIActivityViewController? 如何使用Swift 3更改表格单元格内的标签颜色? - How to change label color inside table cell with swift 3? 按下时更改表格视图中文本的颜色 - Change color of text in tableview when pressed 如何仅在一个表视图单元格中更改标签并使用Swift保存它? - How to change a label in only one table view cell and save it with Swift? 在Swift中进行有效选择时,如何更改具有多个组件的UIPickerView文本的背景颜色 - How do I change the background color of the text in view of the UIPickerView with multiple components when a valid selection is made, in Swift 当按下按钮时,如何获得显示随机文本的标签? [迅速] - How do I get a label to display random text when a button is pressed? [Swift] 滚动视图转到视图2时如何更改文本标签? - How to change text label when scroll view goes to view 2?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM