ios - UITableViewCell UIView 颜色在类加载时不会立即更改

标签 ios swift uitableview

UIView背景颜色在UITableViewCell内没有立即改变, 当类第一次加载时 StatusTableViewCell (TableViewCell) 出现具有默认背景颜色的 backgroundStatusCell (UIView), 即使执行了 guard 条件,backgroundStatusCell 的背景颜色也不会受到影响。仅当单元格滚动时才会发生。

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

        let cell = tableView.dequeueReusableCell(withIdentifier: "StatusTableViewCell") as! StatusTableViewCell
        statusViewModel.configurationStatusCell(cell: cell, forRowAtIndexPath: indexPath as NSIndexPath)

        guard  statusViewModel.progressValues(indexValue: indexPath.row) >= 50 else{

            cell.backgroundStatusView.backgroundColor = .blue
           return cell
        }
        cell.backgroundStatusView.backgroundColor =.red


        return cell
    }
}

最佳答案

试试这个

        let cell = tableView.dequeueReusableCell(withIdentifier: "StatusTableViewCell") as! StatusTableViewCell
        statusViewModel.configurationStatusCell(cell: cell, forRowAtIndexPath: indexPath as NSIndexPath)

        if statusViewModel.progressValues(indexValue: indexPath.row) >= 50
        {
           cell.backgroundStatusView.backgroundColor =.red
           //or
           //cell.backgroundColor =.red
        }
        else{
            cell.backgroundStatusView.backgroundColor = .blue
            //or
           //cell.backgroundColor =.blue
        }
        return cell

关于ios - UITableViewCell UIView 颜色在类加载时不会立即更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58982439/

相关文章:

ios - 如果我们从 segue 获取数据,如何在填写文本字段后启用按钮

ios - 如何以编程方式更改 Swift 中导航栏的高度?

ios - 像在 TableView 行上一样,在节标题上分开 segue

ios - Objective C 中类型的 Typedef NSArray

ios - 字符串包含字母

ios - 将委托(delegate)分配给第三类中另一个类的一个类

ios - stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding) 在 Swift 中返回 Optional

ios - 从完成处理程序中指定单个值

ios - UINavigationController 在 iOS6 中崩溃

ios - UITableView 部分中的部分