我试图使用此方法在表视图中为备用 tableCell 着色链接到颜色单元格 http://blog.apoorvmote.com/customize-uitableviewcell-background-color-ios-8-swift/用这个代码:
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)
}
}
}
但最终对所有单元格进行了着色,如链接所示。
我不确定我是否理解你想要做什么,但是,下面的代码只会为偶数单元格着色(使用你的函数),并用白色绘制奇数单元格
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,代码没有任何出队,单元格的需求和网格只是举例说明了绘制偶数单元格同时对奇数单元格进行缩放的逻辑。我希望这对你有帮助
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)