gpt4 book ai didi

ios交替4种颜色

转载 作者:行者123 更新时间:2023-11-28 19:50:00 25 4
gpt4 key购买 nike

我试图给 4 个单元格不同的颜色,然后重复它。 (所以不是斑马样式表,而是使用 4)为什么这不起作用?我只有 2 种颜色....

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];
}

最佳答案

我将实现以下内容,以便更容易推断出实际发生的情况:

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;
}

关于ios交替4种颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29712340/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com