代码之家  ›  专栏  ›  技术社区  ›  McDonal_11

无法在swift中访问tableviewcell的属性

  •  0
  • McDonal_11  · 技术社区  · 11 年前

    在我的swift应用程序中,我使用的是表视图。选择时,我只能更改单元格的背景色。但是,在牢房里,我有标签。选定单元格时无法更改标签的颜色。如果我为UITableViewCell创建实例,“接收致命错误”

    我的编码在下面。请引导我。如何访问该标签?

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        var cell = tableView.cellForRowAtIndexPath(indexPath)
        cell?.contentView.backgroundColor = UIColor.orangeColor()
        var cell_prop = list_dip_tableView() //Creating instance
        cell_prop.list_label.text.backgroundColor = UIColor.whiteColor()
    //ERROR: fatal error: unexpectedly found nil while unwrapping an Optional value
    }
    
    1 回复  |  直到 11 年前
        1
  •  1
  •   mustafa    11 年前

    UITableViewCell 已选择属性。因此您可以在子类中重写该属性。

    override var selected: Bool {
        didSet {
            if selected { 
                label.textColor = UIColor.redColor()
            } else {
                label.textColor = UIColor.greenColor()
            } 
        } 
    }
    
    推荐文章