我試圖使用此link to color cell與此代碼着色在表視圖備用的TableCell:在UITableView中替代UITableViewCell的顏色?
func colorForIndex(index: Int) -> UIColor
{
let itemCount = stnRepos.count - 1
let color = (CGFloat(index)/CGFloat(itemCount)) * 0.6
return UIColor(red: 0.80, green: color, blue: 0.0, alpha: 1.0)
}
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,
forRowAtIndexPath indexPath: NSIndexPath)
{
var count = stnRepos.count
for (var i = 0; i<=count; i++)
{
if (i % 2 == 0)
{
cell.backgroundColor = colorForIndex(indexPath.row)
println(i)
}
}
}
但最終着色的所有細胞如圖所示的鏈接。
相當明顯。你的計數是什麼? for循環將從0循環到count。即使一旦它進入細胞將被着色。你需要檢查邏輯。 –
stnRepos是我的數組,我將數組的長度賦給count變量。 –