UITableView中的颜色交替UITableViewCell? [英] Color alternate UITableViewCell in a UITableView?

查看:136
本文介绍了UITableView中的颜色交替UITableViewCell?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用此使用此代码链接到颜色单元格:

I was trying to color alternate tableCell in a Table View using this link to color cell with this code:

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)
            }
        }
}

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

but ended up coloring all the cell as shown in the link.

推荐答案

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

I am not sure if I understand what you are trying to do but, the code bellow will just color (using your function) the even cell and paint in white the odd ones

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()()
    }
}

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

ps, the code does not have any the dequeuing and the need and grid from the cells just exemplify the logic to paint the even cell while scaping the odd ones. I hope that helps you

这篇关于UITableView中的颜色交替UITableViewCell?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆