mardi 23 juin 2015

How to Sum floats in a Dynamic NSmutableArray?

I've a problem when doing a sum of Floats inside NSMutableArray.

My NSMutableArray is an array of "Distances that i increase eachtime the tableview get a new row:

This Code is inside :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{


            float D = [location1 distanceFromLocation:location2];

            cell.Distance.text = [NSString stringWithFormat:@"%.2f",D];

            int hours, minutes;

            float minuti,ore;

            ore = D/100;
            hours = (int)ore;
            minuti = ore - hours;
            minuti = minuti * 60;
            minutes = (int)minuti;

            cell.Time.text = [NSString stringWithFormat:@"%02d:%02d h",hours,minutes];

           x = [NSString stringWithFormat:@"%f",D];
           y = [NSString stringWithFormat:@"%f",ore];

            [DistanceArray addObject:x];
            [FlightTimeArray addObject:y];

            [self LoadDistanceAndTime];


return cell       

Now each time i do this function i don't have the correct result:


    float sum = [[DistanceArray valueForKeyPath:@"@sum.floatValue"] floatValue];

    float sum1 = [[FlightTimeArray valueForKeyPath:@"@sum.floatValue"] floatValue];

    TotalDistance.text = [NSString stringWithFormat:@"%.2f",sum];

    int hours, minutes;

    float minuti;

    hours = (int)sum1;
    minuti = sum1 - hours;
    minuti = minuti * 60;
    minutes = (int)minuti;

    TotalFlighttime.text = [NSString stringWithFormat:@"%02d:%02d h",hours,minutes];


how can i fix it ?

See more ..

Aucun commentaire:

Enregistrer un commentaire