Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将浮点赋值到全局数组中_Ios_Objective C_Arrays_Uitableview_Cgfloat - Fatal编程技术网

Ios 将浮点赋值到全局数组中

Ios 将浮点赋值到全局数组中,ios,objective-c,arrays,uitableview,cgfloat,Ios,Objective C,Arrays,Uitableview,Cgfloat,我正在尝试使用渐变来定制分组UITableViewCell的backgroundView,基于代码,我发现它是UIView的一个子类,可用于cell.backgroundView 背景渐变的颜色在原始代码中定义如下: #define TABLE_CELL_BACKGROUND { 1, 1, 1, 1, 0.866, 0.866, 0.866, 1} // #FFFFFF and #DDDDDD 然后,在子类backgroundView的drawRect上这样使用: C

我正在尝试使用渐变来定制分组UITableViewCell的
backgroundView
,基于代码,我发现它是UIView的一个子类,可用于cell.backgroundView

背景渐变的颜色在原始代码中定义如下:

#define TABLE_CELL_BACKGROUND    { 1, 1, 1, 1, 0.866, 0.866, 0.866, 1}          // #FFFFFF and #DDDDDD
然后,在子类backgroundView的
drawRect
上这样使用:

CGFloat components[8] = TABLE_CELL_BACKGROUND;
myGradient = CGGradientCreateWithColorComponents(myColorspace, components , locations, 2);

我正在尝试实现一个函数来设置渐变的开始颜色和结束颜色,该函数使用两种UIColor,然后填充一个全局浮点数组
float startAndEndColors[8]
(in.h/@interface)以供以后使用:

-(void)setColorsFrom:(UIColor*)start to:(UIColor*)end{

    float red = 0.0, green = 0.0, blue = 0.0, alpha =0.0, red1 = 0.0, green1 = 0.0, blue1 = 0.0, alpha1 =0.0;

    [start getRed:&red green:&green blue:&blue alpha:&alpha];
    [end getRed:&red1 green:&green1 blue:&blue1 alpha:&alpha1];

    //This line works fine, my array is successfully filled, just for test
    float colorsTest[8] = {red, green, blue, alpha, red1, green1, blue1, alpha1};

    //But for this one, I just have an error.
    //"Expected expression"
    //                 \
    //                  v
    startAndEndColors = {red, green, blue, alpha, red1, green1, blue1, alpha1};
}
但它在赋值时给了我这个错误“期望表达式”

我尝试了
CGFloat
,拼命地添加random
const
,但我很快就没有主意了


我就是不明白,为什么我不能这样填充我的浮点数组呢?我做错了什么?

添加评论作为答案:


以这种方式创建数组的唯一方法是在代码中动态创建。如果要添加到iVar(类变量),则需要逐个执行,因为在初始化时已经分配了内存。因此,请使用
startAndEndColors[0]=…
,等等

至于您的后续问题:不,没有办法以这种方式将值分配给在分配阶段已经初始化的内存。如果您使用std::vector或其他对象,那么这是可能的

一种方法是在你的标题中这样做

CGFloat *startAndEndColors;
然后在您的实现中执行类似的操作

float colorsTest[8] = {red, green, blue, alpha, red1, green1, blue1, alpha1};
startAndEndColors = colorsTest;

通过这种方式,您可以按照您想要的方式对其进行初始化,但无法保证
startAndEndColors
对象中对象的数量。以后,如果试图访问超出其边界的内容,可能会将其分配给错误大小的内容,并导致崩溃。

以这种方式创建数组的唯一方法是在代码中动态创建数组。如果要添加到iVar(类变量),则需要逐个执行,因为在初始化时已经分配了内存。所以请使用
startAndEndColors[0]=…
,等等。这很有效,谢谢!但是,难道没有一种方法可以像我使用colorsTest那样添加所有这些值吗?请把你的评论换成一个答案,我会接受的。事实上,你的第二个建议似乎有效。另外,当
startAndEndColors
CGFloat*
时,可以直接分配
startAndEndColors=(float[]){red,green,blue,alpha,red1,green1,blue1,alpha1}