2016-02-20 66 views
1

我有兩個數組,一個是所有動物,另一個是selectedMnimals。無論選擇哪種動物,動物的標籤都是不同的顏色。有貓,鯨魚,蝙蝠是白色的,狗和羊是黑色的。如何在特定索引處更改標籤的顏色?

var animals = ["dog", "cat", "sheep", "whale", "bat"] 
var selectedAnimals = ["cat, "whale", "bat"] 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell: selectcell = table.dequeueReusableCellWithIdentifier("Cell") as! selectcell 
    cell.animallabel.text = animals[indexPath.row] 
    cell.selectionStyle = UITableViewCellSelectionStyle.None 
    cell.animallabel.textColor = UIColor.whiteColor 
    return cell 
} 
+0

陣列 '主題' 什麼是更換

cell.animallabel.textColor = UIColor.whiteColor 

? – yesthisisjoe

+0

對不起,我更新了我的代碼。 – stackerleet

回答

1

嘗試

if (selectedAnimals.contains(animal[indexPath.row]) { 
    cell.animallabel.textColor = UIColor.whiteColor 
} else { 
    cell.animallabel.textColor = UIColor.blackColor 
} 
+0

或者更短:'cell.animalLabel.textColor = selectedAnimals.contains(animal [indexPath.row])? .whiteColor():.blackColor()':) – Eendje

相關問題