如何使用iOS sdks在cellForRowAtIndexPath方法中调用Web服务

时间:2015-12-29 07:40:24

标签: ios objective-c uitableview

我想使用 UITableView cellForRowAtIndexPath来呼叫网络服务。如果我滚动tableview它将多次调用,我怎么能避免这种多次Web服务调用。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        WCLTableViewCell *cell = (WCLTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

        ImageRecord *imgRecord = [_dataArray objectAtIndex:indexPath.row];


    NSString *urlStr = [NSString stringWithFormat:@"%@?id=%@&uid=%@",retriveProduct,imgRecord.pid,imgRecord.uid];
                        NSLog(@"url str = %@",urlStr);

                        NSLog(@"service call == %ld", (long)indexPath.row);


                        NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]];
                        NSOperationQueue *queue = [[NSOperationQueue alloc] init];
                        [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
                        {
                                NSDictionary *returnJSON = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

                                    NSURL *aUrlValue = [NSURL URLWithString:[returnJSON objectForKey:@"productImage"]];

                                    NSData *imgDataValues=[NSData dataWithContentsOfURL:aUrlValue];
                                    UIImage *img = [self generatePhotoThumbnail:[UIImage imageWithData:imgDataValues]];
                                    [library writeImageToSavedPhotosAlbum:[img CGImage] orientation:(ALAssetOrientation)[img imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
                                    if (error)
                                    {
                                        localImagePath = @"";
                                    }
                                    else
                                    {
                                        localImagePath = [NSString stringWithFormat:@"%@",assetURL];    
                                        cell.productImage.image = imgRecord.thumbImage;
                                    }

                                    }];
    }

     return cell;
    }

提前致谢。

2 个答案:

答案 0 :(得分:0)

如果您希望Web服务调用只运行一次,您可以将其添加到一次性分派。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

WCLTableViewCell *cell = (WCLTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

ImageRecord *imgRecord = [_dataArray objectAtIndex:indexPath.row];

static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
    NSString *urlStr = [NSString stringWithFormat:@"%@?id=%@&uid=%@",retriveProduct,imgRecord.pid,imgRecord.uid];
    NSLog(@"url str = %@",urlStr);

    NSLog(@"service call == %ld", (long)indexPath.row);


    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]];
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
     {
         NSDictionary *returnJSON = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

         NSURL *aUrlValue = [NSURL URLWithString:[returnJSON objectForKey:@"productImage"]];

         NSData *imgDataValues=[NSData dataWithContentsOfURL:aUrlValue];
         UIImage *img = [self generatePhotoThumbnail:[UIImage imageWithData:imgDataValues]];
         [library writeImageToSavedPhotosAlbum:[img CGImage] orientation:(ALAssetOrientation)[img imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
             if (error)
             {
                 localImagePath = @"";
             }
             else
             {

                 localImagePath = [NSString stringWithFormat:@"%@",assetURL];

                 cell.productImage.image = imgRecord.thumbImage;

                 }

             }];
         }
    });

    return cell;
}

答案 1 :(得分:0)

不止一次调用

cellForRowAtIndexPath:。一般来说,我们只在此方法中设置cell的属性。您可以创建NSArrayNSDictionary,以保存您需要的内容。在cellForRowAtIndexPath中,您可以使用NSArrayNSDictionary来设置cell