我试图保持我的表格行高的 float[] 。我在这里找到了一个很好的类:http ://forums.macnn.com/t/224809/nsmutablearray-vs-a-plain-c-array-for-storing-floats 。
问题是,一旦调用了 C 函数(位于另一个文件中),我传入的浮点数变为 0。这种情况每次都会发生,无论浮点值如何。
C函数:
typedef struct
{
float *array;
int count;
} floatArray;
BOOL AddFloatToArray ( floatArray *farray, float newFloat )
{
if ( farray->count > 0 )
{
// The array is already allocated, just enlarge it by one
farray->array = realloc ( farray->array, ((farray->count + 1) * sizeof (float)) );
// If there was an error, return NO
if (farray->array == NULL)
return NO;
}
else
{
// Allocate new array with the capacity for one float
farray->array = (float *)malloc ( sizeof (float) );
// If there was an error, return NO
if (farray->array == NULL)
return NO;
}
printf("Adding float to array %f\n", newFloat);
farray->array[farray->count] = newFloat;
farray->count += 1;
printArrayContents(farray);
return YES;
}
int printArrayContents( floatArray* farray)
{
printf("Printing array contents\n");
for(int j=0; j < farray->count; j++)
printf("%f\n", farray->array[j]);
return 0;
}
调用自:
NSDictionary* review = [self.reviews objectAtIndex:indexPath.row];
returnHeight = [ReviewCell cellHeightForReview:review];
NSLog(@"Adding height to array: %0.0f", returnHeight);
AddFloatToArray(heights, returnHeight);
以下是记录的内容:
2012-09-28 11:46:12.787 iOS-app[1605:c07] -[ProductDetailViewController tableView:heightForRowAtIndexPath:] [Line 598] Adding height to array: 101
Adding float to array 0.000000
Printing array contents
0.000000
2012-09-28 11:46:12.788 iOS-app[1605:c07] -[ProductDetailViewController tableView:heightForRowAtIndexPath:] [Line 598] Adding height to array: 138
Adding float to array 0.000000
Printing array contents
0.000000
0.000000
2012-09-28 11:46:12.788 iOS-app[1605:c07] -[ProductDetailViewController tableView:heightForRowAtIndexPath:] [Line 598] Adding height to array: 122
Adding float to array 0.000000
Printing array contents
0.000000
0.000000
0.000000
2012-09-28 11:46:12.789 iOS-app[1605:c07] -[ProductDetailViewController tableView:heightForRowAtIndexPath:] [Line 598] Adding height to array: 139
Adding float to array 0.000000
Printing array contents
0.000000
0.000000
0.000000
如何确保将正确的值实际插入到 float[] 中?