我想使用 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;
}
提前致谢。
答案 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
的属性。您可以创建NSArray
或NSDictionary
,以保存您需要的内容。在cellForRowAtIndexPath
中,您可以使用NSArray
或NSDictionary
来设置cell
。