2013-07-01 4 views
0

각 방향 별 셀에서 Google Directions Matrix API를 호출해야하는 스크롤 가능한 UITableView를 만듭니다. 어떤 이유로 스크롤 할 때마다 호출을하고 별도로 계산을 수행합니다여분의 계산 속도가 느려짐 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; 
} 

이 문제를 해결하기 위해 어떤 방법이 있나요, 계산 한 번만 이루어집니다 : 상기 scrolling.Here의 응답에 수신자의 코드입니다.

답변

1

셀에서 이러한 계산을해서는 안됩니다. UITableViewCells는 뷰 레이어의 일부입니다.

컨트롤러에서 요청을 만들어 NSArray와 같은 결과를 저장해야합니다. 그런 다음 cellForRowAtIndexPath는 해당 배열에서 데이터를 가져와야합니다.

관련 문제