在SelectedRow上更改UILabel

时间:2016-10-04 15:15:52

标签: ios swift uitableview

我正在尝试更改所选tableView中单元格的标签颜色。 我试图通过以下代码达到此目的:

TextLabel.HighlightedTextColor = UIColor.White;

不幸的是,这不起作用,并且会保留我从StoryBoard填充的原始颜色。

我将提供完整代码的图片。蓝色背景颜色确实有效,但标签颜色不起作用。

enter image description here

修改

我正在尝试根据是否选择行来更改按钮状态。当一行被选中时,我希望在我没有选择行时显示底部(tableView外部)的按钮(isEnabled = true)我希望它被设置为禁用(isEnabled = false。怎么能我最好处理这个?

目前我有以下逻辑,但最终会出现错误代码崩溃:线程1:EXC_BREAKPOINT(代码= 1,子代码= 0x100044e70)

当我选择一行并取消选择同一行时,应用程序将崩溃。

代码:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        let selectedCell = tableView.cellForRow(at: indexPath)
        selectedCell?.contentView.backgroundColor = UIColor(red: 84.0 / 255, green: 199.0 / 255, blue: 252.0 / 255, alpha: 1)
        self.checkButtonAvailability()
    }

    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        let selectedCell = tableView.cellForRow(at: indexPath)
        selectedCell?.textLabel?.textColor = UIColor.darkGray
        self.checkButtonAvailability()
    }
    @IBAction func nextBtnPressed(_ sender: AnyObject) {
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }

    func checkButtonAvailability() {
        if (self.tableView.indexPathsForSelectedRows?.count)! > 0 {
            nextBtn.isEnabled = true
            nextBtn.backgroundColor = UIColor(red: 84.0 / 255, green: 199.0 / 255, blue: 252.0 / 255, alpha: 1)
        } else {
            nextBtn.isEnabled = false
            nextBtn.backgroundColor = UIColor.darkGray
        }   
    }

2 个答案:

答案 0 :(得分:0)

对您的UITableViewCell进行子类化并将其设置在那里:

class YourTableViewCell: UITableViewCell
{

    @IBOutlet weak var myLabel: UILabel!

    override func setSelected(_ selected: Bool, animated: Bool) {
        if(selected) {            
            self.myLabel.textColor = UIColor.white
        } else {
            self.myLabel.textColor = UIColor(red: 84.0/255.0, green: 199.0/255.0, 252.0/255.0)
        }
    }      
}

然后,您的cellForRow方法可能如下所示:

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

    let cell = ... as! YourTableViewCell 

    ...
    /// you don't need to set the highlight color here. 
}

如果您想跟踪按钮的可用性,请使用 Bool 这样的公共变量(方法之外):

var someRowSelected = false

然后实现某种检查方法:

func checkButtonAvailability() {
    if self.tableView.indexPathsForSelectedRows?.count > 0 {
        myButton.isEnabled = true
    } else {
        myButton.isEnabled = false
    }   
}

然后在您的didSelectRowdidDeselectRow委托方法中,您可以调用该方法:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
   self.checkButtonAvailability()
}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
       self.checkButtonAvailability()
}

答案 1 :(得分:0)

如果您想更改标签颜色,请更改:

selectedCell.textLabel?.HighlightedTextColor = UIColor.white

为:

selectedCell.textLabel.textColor = UIColor.white

这是因为HighlightedTextColor不会改变您的标签颜色。

  

//'突出显示'属性由子类用于诸如   紧迫的状态。将它作为基类的一部分是有用的   用户属性

这是HighlightedTextColor的作用。

希望它会对你有所帮助。