我是iphone开发的新手。我想从数据库中设置uilabel的背景颜色..我已经存储了RGB颜色到数据库中。但是我没有得到背景颜色。
const char *dbpath = [database_Path UTF8String];
sqlite3_stmt *statement;
[category_array removeAllObjects];
[category_arrDictionary removeAllObjects];
if (sqlite3_open(dbpath, &Payer_DB) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat:@"SELECT * FROM Expense_Category"];
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(Payer_DB,query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
// NSLog(@"%i",SQLITE_ROW);
NSMutableString *Category_Id = [[NSMutableString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];
NSMutableString *Category_Name = [[NSMutableString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];
NSMutableString *Category_Image = [[NSMutableString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 2)];
NSMutableString *Category_Color = [[NSMutableString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 3)];
NSLog(@"%@",Category_Color);
[category_arrDictionary setObject:[NSMutableArray arrayWithObjects:Category_Name,Category_Image,Category_Color,nil] forKey:Category_Id];
[category_array addObject:Category_Id];
}
}
else {
NSLog(@"Value updated");
}
sqlite3_finalize(statement);
sqlite3_close(Payer_DB);
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UILabel *lblCatColor = [[UILabel alloc] initWithFrame:CGRectMake(230,10, 70, 40)];
[lblCatColor setBackgroundColor:[[category_arrDictionary valueForKey:[category_array objectAtIndex:indexPath.row]] objectAtIndex:3]];
//lblCatColor.text = [[category_arrDictionary valueForKey:[category_array objectAtIndex:indexPath.row]] objectAtIndex:3];
[cell.contentView addSubview:lblCatColor];
return cell;
}
答案 0 :(得分:2)
试试这个
NSString *strflg=[[category_arrDictionary valueForKey:[category_array objectAtIndex:indexPath.row]] objectAtIndex:3]];
NSArray *colorArray=[strflg componentsSeparatedByString:@","];
float red;
float green;
float blue;
for (int j=0;j<[colorArray count]; j++) {
if (j==0) {
red=[[colorArray objectAtIndex:j] floatValue];
}
else if (j==1)
{
green=[[colorArray objectAtIndex:j] floatValue];
}
else{
blue=[[colorArray objectAtIndex:j] floatValue];
}
}
[lblCatColor setBackgroundColor:[UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:1.0]];
答案 1 :(得分:0)
您需要编写一个小方法来解析从数据库中提取的字符串,并从中创建UIColor
。您不能使用字符串直接设置标签的颜色。
答案 2 :(得分:0)
请尝试使用这个。 你的字典会给你一个字符串,然后你应该分开你的组件,然后把你的红色,绿色和蓝色值放在红色,绿色和蓝色变量的位置。
[lblCatColor setBackgroundColor:[UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:1.0f]];
答案 3 :(得分:0)
它可以帮到你
NSString *strflg=[[category_arrDictionary valueForKey:[category_array objectAtIndex:indexPath.row]] objectAtIndex:3]];
NSArray *colorArray=[strflg componentsSeparatedByString:@","];
[lblCatColor setBackgroundColor:[UIColor colorWithRed:[[colorArray objectAtIndex:0] floatValue]/255.0f green:[[colorArray objectAtIndex:1] floatValue]/255.0f blue:[[colorArray objectAtIndex:2] floatValue]/255.0f alpha:1.0]];
答案 4 :(得分:0)
[[category_arrDictionary valueForKey:[category_array objectAtIndex:indexPath.row]] objectAtIndex:3] returns NSString like 199.0,21.0,133.0.
以下代码可以帮助您......
NSString *colorFormat = [[category_arrDictionary valueForKey:[category_array objectAtIndex:indexPath.row]] objectAtIndex:3];
NSArray *colorArr = [[colorFormat componentsSeparatedByString:@","];
float red = [[colorArr objectAtIndex:0]floatValue];
float green = [[colorArr objectAtIndex:1]floatValue];
float blue = [[colorArr objectAtIndex:2]floatValue];
[lblCatColor setBackgroundColor:[UIColor colorWithRed:(red/255.0) green:(green/255.0) blue:(blue/255.0) alpha:1.0]];
答案 5 :(得分:0)
使用以下代码段从UIColor
NSString
NSString *rgbstr = @"123.0,104.0,238.0";
NSArray *arrRGB = [rgbstr componentsSeparatedByString:@","];
UIColor *color = [UIColor colorWithRed:[arrRGB[0] floatValue]/255.0f green:[arrRGB[1] floatValue]/255.0f blue:[arrRGB[2] floatValue]/255.0f alpha:1.0f];