首先呢 代码方面不是很难,但是你需要理解如图所示,最开始我设置的列表背景颜色是黑色,也就是初始值0. 因为默认0是黑色。
//设置背景颜色
func colorForIndex(index: Int) -> UIColor {
let itemCount = todos.count - 1
let val = (CGFloat(index) / CGFloat(itemCount)) * 0.8
return UIColor(red: 42/255.0, green: 142/252.0, blue: 245/255.0, alpha: 1.0)
}
例如把0/255.0,0/255.0 2/255.0修改为42/255.0, green: 142/252.0, blue: 245/255.0,
另外,你需要添加一段TableView的willDisplayCell的代码:设置每个cell的背景颜色:
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,
forRowAtIndexPath indexPath: NSIndexPath) {
cell.backgroundColor = colorForIndex(indexPath.row)
}