我试着给4个细胞不同的颜色,然后重复。(所以不是斑马样式表,而是使用4)为什么这不起作用?我只得到两种颜色..。
if (indexPath.row % 4 == 0)
[cell setColor:[UIColor colorFromHexString:@"35a8e1"]];
if (indexPath.row % 4 == 1)
[cell setColor:[UIColor colorFromHexString:@"5cb14c"]];
if (indexPath.row % 4 == 2)
[cell setColor:[UIColor colorFromHexString:@"ec292d"]];
else
[cell setColor:[UIColor colorFromHexString:@"ee8c1d"]];
+(UIColor*)colorFromHexString:(NSString*)hex
{
NSString *cString = [[hex stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
// String should be 6 or 8 characters
if ([cString length] < 6) return [UIColor grayColor];
// strip 0X if it appears
if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];
if ([cString length] != 6) return [UIColor grayColor];
// Separate into r, g, b substrings
NSRange range;
range.location = 0;
range.length = 2;
NSString *rString = [cString substringWithRange:range];
range.location = 2;
NSString *gString = [cString substringWithRange:range];
range.location = 4;
NSString *bString = [cString substringWithRange:range];
// Scan values
unsigned int r, g, b;
[[NSScanner scannerWithString:rString] scanHexInt:&r];
[[NSScanner scannerWithString:gString] scanHexInt:&g];
[[NSScanner scannerWithString:bString] scanHexInt:&b];
return [UIColor colorWithRed:((float) r / 255.0f)
green:((float) g / 255.0f)
blue:((float) b / 255.0f)
alpha:1.0f];
}发布于 2015-04-18 03:06:04
试一试:
if (indexPath.row % 4 == 0)
[cell setColor:[UIColor colorFromHexString:@"35a8e1"]];
else if (indexPath.row % 4 == 1)
[cell setColor:[UIColor colorFromHexString:@"5cb14c"]];
else if (indexPath.row % 4 == 2)
[cell setColor:[UIColor colorFromHexString:@"ec292d"]];
else
[cell setColor:[UIColor colorFromHexString:@"ee8c1d"]];发布于 2015-04-18 03:06:28
为了更容易地扣减实际发生的情况,我将实现以下几点:
NSUInteger index = indexPath.row;
if (index % 4 == 0) {
[cell setColor:[UIColor colorFromHexString:@"35a8e1"]];
} else if (index % 4 == 1) {
[cell setColor:[UIColor colorFromHexString:@"5cb14c"]];
} else if (index % 4 == 2) {
[cell setColor:[UIColor colorFromHexString:@"ec292d"]];
} else {
[cell setColor:[UIColor colorFromHexString:@"ee8c1d"]];
}当以这种方式编写时,您的代码被迫只执行一个操作。
另外,我将在第一个if语句上设置一个断点,并逐步遍历整个语句,以查看要执行哪个语句。
或者,使用switch语句可能会使事情变得更清楚一些:
switch (indexPath.row % 4) {
case 0:
[cell setColor:[UIColor colorFromHexString:@"35a8e1"]];
break;
case 1:
[cell setColor:[UIColor colorFromHexString:@"5cb14c"]];
break;
case 2:
[cell setColor:[UIColor colorFromHexString:@"ec292d"]];
break;
case 3:
[cell setColor:[UIColor colorFromHexString:@"ee8c1d"]];
break;
}https://stackoverflow.com/questions/29712340
复制相似问题