-1

我是 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;
}
4

6 回答 6

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]];
于 2013-05-10T06:30:27.067 回答
0

使用以下代码片段UIColorNSString

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];
于 2013-05-10T07:25:51.787 回答
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]];
于 2013-05-10T06:34:01.253 回答
0

您需要编写一个小方法来解析您从数据库中提取的字符串并UIColor从中创建一个。您不能使用字符串直接设置标签的颜色。

于 2013-05-10T06:28:20.810 回答
0

请尝试使用这个。你的字典会给你一个字符串,然后你应该分开你的组件,然后把你的红色、绿色和蓝色值放在红色、绿色和蓝色变量的位置。

[lblCatColor setBackgroundColor:[UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:1.0f]];
于 2013-05-10T06:29:53.333 回答
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]];
于 2013-05-10T06:39:09.287 回答