在 Parse 中从对应于 Color 的索引路径设置 Cell Background Color
Set Cell Background Color from index path corresponding Color in Parse
我有一个 Table View
,我想根据存储在 Parse
[= 中的相应颜色 为 cell
的背景着色47=].
我不知道如何从 array
s 对应的单元格中抓取颜色并使其成为单元格的背景色。
有什么想法吗? post 是否需要任何额外的代码,谢谢!
这是我对没有背景色的 table 的看法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{
static NSString *simpleTableIdentifier = @"RecipeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
UILabel *homeLabel = (UILabel*) [cell viewWithTag:101];
homeLabel.text = [object objectForKey:@"matchup"];
UILabel *dateLabel = (UILabel*) [cell viewWithTag:102];
dateLabel.text = [object objectForKey:@"time"];
return cell;
}
这是我对 Parse
进行的查询的查询结果的颜色数组:
这是我已经保存了 Parse
查询中的颜色的数组变量:
// Color Cell
NSLog(@"Color array global: %@", self.stringArrayColor);
// Output in console
2015-02-19 19:07:52.964 SimpleTable[12628:1448286] Color array global: (
006532,
061642
)
这是我必须转换十六进制字符串颜色的方法:
// Added to convert Hex colors to RGB
-(UIColor*)colorWithHexString:(NSString*)hex
{
NSString *cString = [[hex stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
// String should be 6 or 8 characters
if ([cString length] < 6) return [UIColor grayColor];
// MATT ADDED THIS SO IF COLOR RETURNED NULL, THEN THE COLOR WOULD BE SET TO ICON COLOR NAVY BLUE, SAME AS TINT COLOR
if ([cString hasPrefix:@"("]) return [UIColor colorWithRed:7.0f/255.0f green:32.0f/255.0f blue:50.0f/255.0f alpha:1.0f];
// 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];
}
这是我之前使用过的方法类型的示例 method
,如果对您有帮助,仅供参考:
[buttonOne setBackgroundColor:[self colorWithHexString:[NSString stringWithFormat:@"%@", theColor]]];
只需使用您提供的示例:
cell.backgroundColor = [self colorWithHexString:[self.stringArrayColor objectAtIndex:indexPath.row]];
我有一个 Table View
,我想根据存储在 Parse
[= 中的相应颜色 为 cell
的背景着色47=].
我不知道如何从 array
s 对应的单元格中抓取颜色并使其成为单元格的背景色。
有什么想法吗? post 是否需要任何额外的代码,谢谢!
这是我对没有背景色的 table 的看法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{
static NSString *simpleTableIdentifier = @"RecipeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
UILabel *homeLabel = (UILabel*) [cell viewWithTag:101];
homeLabel.text = [object objectForKey:@"matchup"];
UILabel *dateLabel = (UILabel*) [cell viewWithTag:102];
dateLabel.text = [object objectForKey:@"time"];
return cell;
}
这是我对 Parse
进行的查询的查询结果的颜色数组:
这是我已经保存了 Parse
查询中的颜色的数组变量:
// Color Cell
NSLog(@"Color array global: %@", self.stringArrayColor);
// Output in console
2015-02-19 19:07:52.964 SimpleTable[12628:1448286] Color array global: (
006532,
061642
)
这是我必须转换十六进制字符串颜色的方法:
// Added to convert Hex colors to RGB
-(UIColor*)colorWithHexString:(NSString*)hex
{
NSString *cString = [[hex stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
// String should be 6 or 8 characters
if ([cString length] < 6) return [UIColor grayColor];
// MATT ADDED THIS SO IF COLOR RETURNED NULL, THEN THE COLOR WOULD BE SET TO ICON COLOR NAVY BLUE, SAME AS TINT COLOR
if ([cString hasPrefix:@"("]) return [UIColor colorWithRed:7.0f/255.0f green:32.0f/255.0f blue:50.0f/255.0f alpha:1.0f];
// 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];
}
这是我之前使用过的方法类型的示例 method
,如果对您有帮助,仅供参考:
[buttonOne setBackgroundColor:[self colorWithHexString:[NSString stringWithFormat:@"%@", theColor]]];
只需使用您提供的示例:
cell.backgroundColor = [self colorWithHexString:[self.stringArrayColor objectAtIndex:indexPath.row]];