冗余计算减慢了UITableView的速度

时间:2013-07-01 18:39:46

标签: ios uitableview tableview

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

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

1 个答案:

答案 0 :(得分:1)

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

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