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, 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
Just to be sure...
Are you trying to style your tableview so that odd rows have one background color and even rows have another?
If that is the case, then perhaps this could help you:
Change color of alternate row in UITableView for iPhone SDK
Regarding your code and why it behaves as it does...
- tableView:willDisplayCell:forTableColumn:row:
gets called once for each row in your tableview, just before it is about to be displayed.
In each of these calls you loop through your entire array, and if the current value of i is even, you color the background of your table cell, meaning that when i is 0, you color your background, when i is 2 you do the same, and you do this for each row...this is probably not what you're interested in :-)
As Peter Tutervai suggests, you should check if tableColumn is odd or even and act on that
The problem with your code is that you have a single cell which you are coloring stnRepos.count
times. You should change your code to something like this:
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()
}
}
Edit: Added an else
case as mentioned by @IcaroNZ to keep the expected behavior even if cells are dequeued
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,
forRowAtIndexPath indexPath: NSIndexPath)
{
if (indexPath.row % 2 == 0)
{
cell.backgroundColor = UIColor.gray
} else {
cell.backgroundColor = UIColor.white
}
}
try changing
cell.backgroundColor = colorForIndex(indexPath.row)
To
cell.contentView.backgroundColor = colorForIndex(indexPath.row)