0
我正在創建一個可滾動的UITableView,其中每個單元格都需要調用Google Directions Matrix API。出於某種原因,每次我滾動它時都會進行調用並分別進行顯着的計算在scrolling.Here的響應收費是代碼:冗餘計算減慢UITableView
- (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;
}
有什麼辦法來解決這個問題,其中,計算只需進行一次。