我要做的是在来自UICollectionView
的{{1}}中呈现图像,该图像来自服务器作为响应。 imageUrl
没有问题,因为我已使用UICollectionView
对其进行了测试。当我尝试使用hardcoded URL
作为键值对加载array
时,问题就开始了。可能是,我不知道我必须打电话给url
的确切位置。我完全按照应有的方式获得响应并将其传递给数组。在调试时我可以看到method which is responsible for getting the response from the server
UICollectionView委托方法after getting response control is not going back to execute the
imageUrl`。这是我正在尝试的代码。
to render
响应
#import "ProductCollectionViewController.h"
#import "ProductCell.h"
#import "UIImageView+WebCache.h"
@interface ProductCollectionViewController ()
{
NSMutableData *receivedData;
NSMutableArray *productList;
}
@end
@implementation ProductCollectionViewController
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
static NSString * const reuseIdentifier = @"Cell";
-(void)viewDidLoad
{
[super viewDidLoad];
[self getProductList];
}
#pragma mark <UICollectionViewDataSource>
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return productList.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
ProductCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
NSURL *url = [[productList objectAtIndex:indexPath.row]valueForKey:@"url"];
[cell.productImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder.jpg"]];
return cell;
}
-(void)getProductList
{
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: self delegateQueue: [NSOperationQueue mainQueue]];
NSURL * url = [NSURL URLWithString:@“http:xxxxxxxxx"];
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];
NSURLSessionDataTask * dataTask = [defaultSession dataTaskWithRequest:urlRequest];
[dataTask resume];
}
-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
{
NSHTTPURLResponse *httpResp = (NSHTTPURLResponse*) response;
NSLog(@"status code %li", httpResp.statusCode);
receivedData=nil;
receivedData=[[NSMutableData alloc] init];
[receivedData setLength:0];
completionHandler(NSURLSessionResponseAllow);
}
-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
{
[receivedData appendData:data];
}
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
if (error) {
// Handle error
}
else {
NSError *tempError;
NSDictionary* response=(NSDictionary*)[NSJSONSerialization JSONObjectWithData:receivedData options:kNilOptions error:&tempError];
NSLog(@"Response is :%@", response);
productList = [NSMutableArray new];
NSArray *rsBody = response [@"rsBody"];
for(NSDictionary *dict in rsBody)
{
[productList addObject:@{@“url":dict[@"productImageUrl"]}]
}
}
}
答案 0 :(得分:0)
更改此行
NSMutableURLRequest * urlRequest = [NSMutableURLRequest
requestWithURL:url];
到
NSURLRequest * urlRequest = [NSURLRequest requestWithURL:url];
休息看起来不错
答案 1 :(得分:0)
请尝试以下代码。希望它为你工作。
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
ProductCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
NSURL *url = [NSURL URLWithString:[[productList objectAtIndex:indexPath.row]valueForKey:@"url"]];
[cell.productImageView sd_setImageWithURL:url
placeholderImage:[UIImage imageNamed:@"placeholder"] options:SDWebImageRefreshCached
completed:nil];
return cell;
}
谢谢:)