【问题标题】:Swift: Can't edit properties of custom table cell programmaticallySwift:无法以编程方式编辑自定义表格单元格的属性
【发布时间】:2019-03-23 16:15:58
【问题描述】:

我正在尝试更改自定义表格单元格中视图的颜色,并且我有一个出口,它可以工作。我可以更改此视图的其他属性,例如 .isHidden.backgroundColor 似乎不起作用。知道我做错了什么吗?

UIColor(named: "Green") 适用于应用程序的其他部分,但我也无法使用它更改文本的颜色。我是否分配了错误的颜色类型?故事板中的值是否只是覆盖了这个?如果是这样,我怎么能阻止这种情况发生?将其更改为= .red 也不起作用。

代码如下:

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


    let cell = tableView.dequeueReusableCell(withIdentifier: "ChapterCell") as! ChapterCell
    smallLabel(cell.chapterLabel, 18)
    cell.keepSubviewBackground = true

    if chapters[indexPath.row].completed == true {
        cell.chapterNumber.isHidden = true
        cell.chapterTick.isHidden = false
        cell.chapterLabel?.text = chapters[indexPath.row].generateTitle()
        cell.chapterLabel?.textColor = UIColor(named: "Green")
        cell.chapterNumberContainer.backgroundColor = UIColor(named: "Green")
    } else {
        cell.chapterLabel?.text = chapters[indexPath.row].generateTitle()
        cell.chapterNumber?.text = "#\(indexPath.row + 1)"
        cell.chapterTick.isHidden = true
        cell.chapterNumber.isHidden = false
        cell.chapterNumberContainer.backgroundColor = UIColor(named: "Eggshell")
    }

    if chapters[indexPath.row].locked == true {
        cell.chapterLabel?.alpha = 0.3
        cell.chapterNumberContainer?.alpha = 0.3
    } else {
        cell.chapterLabel?.alpha = 1
        cell.chapterNumberContainer?.alpha = 1
    }

    let cellBGView = UIView()
    cellBGView.backgroundColor = UIColor(red: 1.00, green: 1.00, blue: 1.00, alpha: 0.1)
    cell.selectedBackgroundView = cellBGView

    return cell
}

【问题讨论】:

  • 嗯,感谢@GaloTorresSevilla,视图是在情节提要中创建的,并且可以正确渲染。问题是颜色没有变化。我有一个视图的出口,所以我认为这会改变它的背景颜色。例如,我可以使用 .isHidden 使用插座来隐藏它。 dropbox.com/s/8z95rb2ozdgmfnt/…关于该项目的任何其他细节会有所帮助吗?
  • 不要在表的数据源中实例化视图。使用可重用单元格时,数据源应尽可能轻量级。它应该做的就是向单元格注入数据。在别处做所有繁重的工作。在选定背景的情况下,在单元格本身中对其进行编码。
  • 考虑制作两个单元格,每个单元格都以自己的方式设置样式,并根据数据将数据源中所需的单元格出列。尽量不要为数据源中的单元格设置样式,尤其是在单元格的层次结构中切换视图,自动布局会讨厌你,你的滚动性能可能会受到明显影响,而且肯定会出现问题。
  • 看来这就是我必须做的,谢谢。

标签: swift uitableview uiview uibackgroundcolor


【解决方案1】:

检查一下,这里可能是个问题。 UITableViewCell textLabel color not changing 注意:不能因为声誉较低而发表评论。这就是为什么将其发布为答案。

【讨论】:

  • 谢谢,但这似乎没有任何区别。我尝试将 isUserInteractionEnabled = false (或 true)添加到单元格和视图中,并且颜色仍然不会改变。其他所有属性都可以正常工作,只是由于某种原因不是颜色。还是我误解了您建议的答案。
【解决方案2】:

非常感谢所有建议,我最终选择了 bsod 建议的解决方案,我想为将来需要此功能的任何人提供更多详细信息。 Rob 的解决方案对我不起作用。

  1. 复制表格视图中的第一个自定义单元格并根据需要设置样式。

  1. 创建第二个类并将其分配给第二个单元格,以及一个新标识符。您还可以在第二类中创建您需要的任何出口,它们可以具有相同的名称。

  1. 在 cellForRowAt indexPath 方法中,我使用 if 语句根据章节是否完成将自定义单元格出列,然后相应地设置它们的样式。这会重复一些代码,但单元格必须进行类型转换才能工作(如果有人能想出更好的方法,我愿意接受替代方案)。

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

    if chapters[indexPath.row].completed == false {
        let cell = tableView.dequeueReusableCell(withIdentifier: "RegularCell") as! RegularCell
    
        cell.chapterLabel?.text = chapters[indexPath.row].generateTitle()
        cell.chapterNumber?.text = "#\(indexPath.row + 1)"
    
        if chapters[indexPath.row].locked == true {
            cell.chapterLabel?.alpha = 0.3
            cell.chapterNumberContainer?.alpha = 0.3
        } else {
            cell.chapterLabel?.alpha = 1
            cell.chapterNumberContainer?.alpha = 1
        }
    
        let cellBGView = UIView()
        cellBGView.backgroundColor = UIColor(red: 1.00, green: 1.00, blue: 1.00, alpha: 0.1)
        cell.selectedBackgroundView = cellBGView
        smallLabel(cell.chapterLabel, 18)
        cell.keepSubviewBackground = true
    
        return cell
    } else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CompletedCell") as! CompletedCell
    
        cell.chapterLabel?.text = chapters[indexPath.row].generateTitle()
    
        let cellBGView = UIView()
        cellBGView.backgroundColor = UIColor(red: 1.00, green: 1.00, blue: 1.00, alpha: 0.1)
        cell.selectedBackgroundView = cellBGView
        smallLabel(cell.chapterLabel, 18)
        cell.keepSubviewBackground = true
    
        return cell
    }
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-30
    • 1970-01-01
    • 2015-05-14
    • 2020-02-08
    相关资源
    最近更新 更多