2015-11-04 54 views
1

我想要做的是從imageUrlUICollectionView中渲染圖像,該圖像來自服務器作爲響應。 UICollectionView沒有問題,因爲我已經用hardcoded URL進行了測試。當我嘗試使用包含url作爲鍵值對的array加載它時,問題就開始了。可能是,我不知道從哪裏撥打method which is responsible for getting the response from the server的確切位置。我完全按照應該的方式獲取響應,並將其傳遞到數組中。在調試時,我可以看到after getting response control is not going back to execute the UICollectionView委託方法to render imageUrl`。這是我正在嘗試的代碼。使用服務器響應在UICollectionView中顯示來自imageURL的圖像

#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"]}] 
    } 

    } 

} 

響應 {"rsBody": [{"productId":11, "productImageUrl":"http:xxxx"}, {"productId":9, "productImageUrl":"http:"xxxx"}]}

+0

能否請您發送您的API響應? –

+0

plz檢查,更新了問題。 –

+0

請檢查我更新的答案,希望它的工作。 –

回答

0

改變這一行從 NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:URL]; 到

NSURLRequest * urlRequest = [NSURLRequest requestWithURL:url]; 

其餘看起來不錯

+0

我認爲它不會影響任何東西。 –

+0

休息看起來很好,當我遇到類似的問題時我做了這個,對我很好用 –

+0

如果可能是問題,我不會得到響應。NSURLRequest和NSMutableURLRequest幾乎是一樣的。 –

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; 
} 

謝謝:)

+0

問題是控件不會去那個委託方法 –

+0

您可以請分享您的API響應或Image URL? –

+0

什麼是'[NSURL URLWithString:imgUrl]'中的'imgUrl''我已經在't中附加'productImageUrl'他陣列。 –

相關問題