UITableView中的颜色交替UITableViewCell?

Irs*_*shi 3 uitableview ios uibackgroundcolor swift

我试图使用此链接将表格视图中的替换tableCell 着色为此代码的颜色单元格:

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)
            }
        }
}
Run Code Online (Sandbox Code Playgroud)

但最终着色所有单元格,如链接所示.

Ica*_*aro 11

我不确定我是否理解你要做的事情,但是,下面的代码只是颜色(使用你的函数)偶数单元格和白色涂漆奇数代码

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) 
{        
    if (indexPath.row % 2 == 0)
    {
        cell.backgroundColor = colorForIndex(indexPath.row)
    } else {
        cell.backgroundColor = UIColor.whiteColor()()
    }
}
Run Code Online (Sandbox Code Playgroud)

ps,代码没有任何出列,并且来自单元格的需要和网格只是例证了在绘制奇数单元格时绘制偶数单元格的逻辑.我希望能帮助你