2017-07-07 33 views
1

我在列表中使用了一個dequeueReusableCell,並且單元格中有4個UILabel。下面有截圖。如何標記dequeueReusableCell而不會弄亂內容?

無論標籤的前綴是否爲「 - 」,它都是紅色的,但在滾動幾頁之後,數字就變得混亂了。想知道是否有辦法避免這種情況?

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

+0

用您的'cellForRowAt'方法編輯您的問題。 – rmaddy

+0

向我們展示您的代碼,從您管理標籤的顏色基於前綴' - '的位置。 – Surjeet

+2

細胞被重複使用。在'cellForRow'中添加一個'else'子句,以明確地將顏色設置爲黑色,如果該值爲**不是**否定的。 – vadian

回答

1
在cellForRowAt方法

你需要檢查,如果值是正的顏色黑色,如果是負面的,它是紅色的。例如

label.color = black 

if negativeValue { 
    lagel.color = red 
}