3

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.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Irshad Qureshi
  • 807
  • 18
  • 41
  • 1
    quite obvious. what is your count? for loop will cycle from 0 through count. and even if once it enters if cell will be colored. you need to check the logic. – Vivek Molkar May 28 '15 at 11:38
  • stnRepos is my array and I am assigning the length of the array to the count variable..!! – Irshad Qureshi May 28 '15 at 11:47

5 Answers5

14

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

Icaro
  • 14,585
  • 6
  • 60
  • 75
1

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

Peter Tutervai
  • 793
  • 12
  • 21
  • Superb..!!! worked Fine...!!! Got what I needed...!!! Thanks...!!! I have completed my all votes today but tomorrow i'll surely upvote this answer...!! – Irshad Qureshi May 28 '15 at 12:04
  • 2
    @IrshadQureshi don't forget that if you are dequeuing cells you will need to repaint the cell out of the if statement as the reusable cell have the old settings cached on it. – Icaro May 28 '15 at 12:11
1

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

Community
  • 1
  • 1
pbodsk
  • 6,787
  • 3
  • 21
  • 51
0
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,
    forRowAtIndexPath indexPath: NSIndexPath) 
   {        
 if (indexPath.row % 2 == 0)
 {
     cell.backgroundColor = UIColor.gray
 } else {
    cell.backgroundColor = UIColor.white
 }

}

Sai kumar Reddy
  • 1,751
  • 20
  • 23
0

try changing

cell.backgroundColor = colorForIndex(indexPath.row)

To

cell.contentView.backgroundColor = colorForIndex(indexPath.row)
Karan Pal
  • 67
  • 5