Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.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 冗余计算降低了UITableView的速度_Ios_Uitableview_Tableview - Fatal编程技术网

Ios 冗余计算降低了UITableView的速度

Ios 冗余计算降低了UITableView的速度,ios,uitableview,tableview,Ios,Uitableview,Tableview,我正在创建一个可滚动的UITableView,其中每个单元格都需要调用Google Directions Matrix API。出于某种原因,每次我滚动时,它都会进行调用并单独进行计算,这会严重影响滚动的响应性。下面是代码: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if(tableView.tag == 1) { r

我正在创建一个可滚动的UITableView,其中每个单元格都需要调用Google Directions Matrix API。出于某种原因,每次我滚动时,它都会进行调用并单独进行计算,这会严重影响滚动的响应性。下面是代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  if(tableView.tag == 1) {
    return [self specialTableViewCell: tableView];
}

static NSString *CellIdentifier = @"OfferCell";
OTNOfferCell *cell = (OTNOfferCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)
{
    // create cell using style Subtitle
    cell = [[OTNOfferCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}

PFObject *venue = [self.venues objectAtIndex:indexPath.section];

cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MainPage Item.png"]];
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MainPageItemSelected.png"]];

cell.clubNameLabel.text = [venue valueForKey: @"name"] ;

PFFile *photo = [venue objectForKey:@"logo"];
NSData *data = [photo getData];
UIImage *image = [UIImage imageWithData: data];
cell.clubLogoImageView.image = image;

int count = [[venue valueForKey:@"events"] count];


if(count == 1)
{

    cell.numberOfEventsLabel.text = @"1 Event";
}
else
{
    cell.numberOfEventsLabel.text = [NSString stringWithFormat:@"%d Events", count];
}
PFGeoPoint *destinationLocation = [venue objectForKey:@"geopoint"];
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
[locationManager startUpdatingLocation];
PFGeoPoint *currentLocation = [PFGeoPoint geoPointWithLatitude:locationManager.location.coordinate.latitude longitude:locationManager.location.coordinate.longitude];

NSString *current = [venue objectForKey:@"address"];
  NSLog(@"%@",current);
    NSString *urlString = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/distancematrix/json?origins=%@&destinations=San+Francisco&mode=driving&language=en&sensor=true&units=imperial",current];
    NSURL *url = [NSURL
                  URLWithString:[urlString
                                 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    NSData *googledata = [NSData dataWithContentsOfURL:url];
    NSError *error;
    NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:googledata options:kNilOptions error:&error];



    NSString *result = [[[[[[json objectForKey:@"rows"] objectAtIndex: 0] objectForKey:@"elements"] objectAtIndex:0]objectForKey:@"distance"]objectForKey:@"text"];




double tempd = [currentLocation distanceInMilesTo:destinationLocation];
NSString *distance = [NSString stringWithFormat:@"%f",tempd];
NSString *distanceTrunc = [distance substringToIndex: MIN(3, [distance length])];

cell.distanceLabel.text = [NSString stringWithFormat:@"%@ mi", distanceTrunc];

return cell;
}

如果计算只进行一次,是否有办法解决此问题。

您不应该在单元格中进行这些计算。UITableViewCells是视图层的一部分

您应该在控制器中发出请求,并将结果存储在类似NSArray的东西中。然后cellForRowAtIndexPath应该只从该数组中提取数据