Search code examples
ioscolorscell

iOS change cell color ( based on row)


I want the background color of my cells to change depending on the row. I'm using this code. Why are all cells red?

CGFloat proportion = indexPath.row/(float)rows;
cell.backgroundColor = [UIColor colorWithRed:(proportion*255) green:0.0f  blue:0.0f alpha:1.0f];

Screenshot 1

But if I use this code the color show normally

cell.backgroundColor = [UIColor colorWithWhite:proportion alpha:1.0f];

Screenshot 2


Solution

  • U count it wrong

    U proportion * 255 is bigger than 1

    use just proportion