iOS 更改单元格颜色(基于行)

iOS change cell color ( based on row)

我希望单元格的背景颜色根据行的不同而变化。我正在使用这段代码。为什么所有单元格都是红色的?

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

Screenshot 1

但是如果我使用这段代码颜色显示正常

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

Screenshot 2

你算错了

U比例*255大于1

只使用比例

不要与255相乘,颜色值在[0,1.0]范围内

cell.backgroundColor = [UIColor colorWithRed:proportion green:0.0f blue:0.0f alpha:1.0f];
[UIColor colorWithRed:red green:green blue:blue alpha:1.0f];

redgreenbluealpha参数在0.0~1.0之间。如果超过1.0,则按1.0处理。