ios - 如何在不弄乱内容的情况下标记 dequeueReusableCell?

标签 ios swift uitableview tableview

我在一个列表中使用了一个dequeueReusableCell,单元格中有4个UILabel。下面有截图。

凡是有“-”字头的标签,都是红色的,但翻了几页,数字就乱七八糟了。想知道是否有办法避免这种情况?

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "RecipeTableCell", for: indexPath)
    let currentRecipe = recipes?[indexPath.row]
    let colorRed = UIColor(red: 0xFE/255, green: 0x38/255, blue: 0x24/255, alpha: 1) //FE3824

    (cell.viewWithTag(100) as! UIImageView).image = UIImage(named: currentRecipe?.pic ?? "")
    (cell.viewWithTag(200) as! UILabel).text = currentRecipe?.name

    let hungryLabel = cell.viewWithTag(301) as! UILabel
    hungryLabel.text = currentRecipe?.hungry_value
    if (currentRecipe?.hungry_value!.hasPrefix("-"))! {
        hungryLabel.textColor = colorRed
    }

    let healthLabel = (cell.viewWithTag(302) as! UILabel)
    healthLabel.text = currentRecipe?.health_value
    if (currentRecipe?.health_value!.hasPrefix("-"))! {
        healthLabel.textColor = colorRed
    }

    let sanityLabel = (cell.viewWithTag(303) as! UILabel)
    sanityLabel.text = currentRecipe?.sanity_value
    if (currentRecipe?.sanity_value!.hasPrefix("-"))! {
        sanityLabel.textColor = colorRed
    }

    (cell.viewWithTag(304) as! UILabel).text = currentRecipe?.duration

    return cell
}

Shot Before Scroll

Shot After scroll


谢谢你们。

I add a Black Color to 'else', it works, thank you

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "RecipeTableCell", for: indexPath)
    let currentRecipe = recipes?[indexPath.row]
    let colorRed = UIColor(red: 0xFE/255, green: 0x38/255, blue: 0x24/255, alpha: 1)    //FE3824
    let colorBlack = UIColor(red: 74/255, green: 74/255, blue: 74/255, alpha: 1)        //747474

    (cell.viewWithTag(100) as! UIImageView).image = UIImage(named: currentRecipe?.pic ?? "")
    (cell.viewWithTag(200) as! UILabel).text = currentRecipe?.name

    let hungryLabel = cell.viewWithTag(301) as! UILabel
    hungryLabel.text = currentRecipe?.hungry_value
    if (currentRecipe?.hungry_value!.hasPrefix("-"))! {
        hungryLabel.textColor = colorRed
    } else {
        hungryLabel.textColor = colorBlack
    }

    let healthLabel = (cell.viewWithTag(302) as! UILabel)
    healthLabel.text = currentRecipe?.health_value
    if (currentRecipe?.health_value!.hasPrefix("-"))! {
        healthLabel.textColor = colorRed
    } else {
        healthLabel.textColor = colorBlack
    }

    let sanityLabel = (cell.viewWithTag(303) as! UILabel)
    sanityLabel.text = currentRecipe?.sanity_value
    if (currentRecipe?.sanity_value!.hasPrefix("-"))! {
        sanityLabel.textColor = colorRed
    } else {
        sanityLabel.textColor = colorBlack
    }

    (cell.viewWithTag(304) as! UILabel).text = currentRecipe?.duration

    return cell
}

Works now

最佳答案

在您的 cellForRowAt 方法中,您需要检查值是否为正,颜色为黑色,如果为负,则颜色为红色。例如

label.color = black

if negativeValue {
     lagel.color = red
}

关于ios - 如何在不弄乱内容的情况下标记 dequeueReusableCell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44962181/

相关文章:

ios - 使用实例填充 UITableview 来填充数组

ios - 如何将表数据源和委托(delegate)导出链接到 UITableViewController?

ios - 使用 Magical Record 添加到创建的实体属性

ios - 实现委托(delegate)

objective-c - 在避免使用NSTimer的同时,如何实现“dayDidChange”方法?

swift - 进程无法从 Swift 中的标准输入读取

ios - Cosmic mind - 如何更改标题栏按钮的色调

ios - Spritekit游戏在iPad模拟器上出现严重滞后

ios - 如何在初始化后更新自定义类中的 UILabel 文本 (Swift 3)

ios - 点击时更改 UITableViewCell 高度时动画不稳定