如何标记dequeueReusableCell而不会弄乱内容?

时间:2017-07-07 03:43:25

标签: 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

谢谢你们。

  

我将“黑色”添加到“其他'”,它有效,谢谢

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

1 个答案:

答案 0 :(得分:1)

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

label.color = black

if negativeValue {
     lagel.color = red
}