简体   繁体   English

Swift4:如何更改 UITableView 单元格颜色

[英]Swift4: How to change UITableView cell color

I have a UITable view where you can add items by pressing a button.我有一个 UITable 视图,您可以在其中按按钮添加项目。 I want its cells to be clear, I already made its background semitransparent, but once you click on the button that allows you to save the items in it, the first you save has a white background.我希望它的单元格清晰,我已经将其背景设为半透明,但是一旦您单击允许您保存其中项目的按钮,您保存的第一个背景就会变成白色。 Then, if you press the button other times, all of the cells, except for the last created, become semitransparent, but one continues to be white.然后,如果您在其他时间按下按钮,除最后创建的单元格之外的所有单元格都将变为半透明,但一个仍为白色。 This is my code, how could I make it completely clear?这是我的代码,我怎样才能让它完全清楚?

extension ViewController : UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return number.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let textCell = tableView.dequeueReusableCell(withIdentifier: "WordCell", for: indexPath)
        textCell.textLabel?.text = "\(indexPath.row + 1)         \(number[indexPath.row])"
        let semitransparentBlack = UIColor(rgb: 0x000000).withAlphaComponent(0.3)
        textCell.layer.backgroundColor = semitransparentBlack.cgColor
        textCell.textLabel?.layer.backgroundColor = semitransparentBlack.cgColor
        textCell.textLabel?.textColor = UIColor.white
        return textCell

    }


}

您可以使用textCell.backgroundColor = semitransparentBlack设置颜色

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM