0

我正在创建一个可滚动的 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;
}

有没有办法解决这个问题,其中计算只进行一次。

4

1 回答 1

1

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

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

于 2013-07-01T21:20:08.213 回答