2014-06-10 88 views
0

我解析了將JSON返回給我的iOS應用程序的雲代碼。從下面的JSON中可以看出,每個項目都有一個Image URL屬性。我想要做的是將每個UITableViewCell的縮略圖設置爲該項目各自的Image URL如何顯示以JSON形式返回的圖像URL,作爲UITableViewCell的縮略圖

我知道這涉及到處理異步行爲,因此單元必須等待圖像加載後再顯示它們。我一直在搜索如何完成這項任務,而且我無法理解如何去做。

我下面用

NSData *imageData = [NSData dataWithContentsOfURL:[NSURL [*matchCenterDictionary objectForKey:@"Image URL"]]]; 
    [[cell imageView] setImage:[UIImage imageWithData:imageData]]; 

試圖給我的錯誤,說明expected identifier

MatchCenterViewController.m:返回

#import "MatchCenterViewController.h" 
#import <UIKit/UIKit.h> 

@interface MatchCenterViewController() <UITableViewDataSource, UITableViewDelegate> 
@property (nonatomic, strong) UITableView *matchCenter; 
@end 

@implementation MatchCenterViewController 


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
    } 
    return self; 
} 


- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.matchCenter = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewCellStyleSubtitle]; 
    self.matchCenter.frame = CGRectMake(0,50,320,self.view.frame.size.height-200); 
    _matchCenter.dataSource = self; 
    _matchCenter.delegate = self; 
    [self.view addSubview:self.matchCenter]; 

    self.matchCenterArray = [[NSArray alloc] init]; 
} 

- (void)viewDidAppear:(BOOL)animated 
{ 
    self.matchCenterArray = [[NSArray alloc] init]; 

    [PFCloud callFunctionInBackground:@"MatchCenter" 
         withParameters:@{ 
             @"test": @"Hi", 
             } 
           block:^(NSDictionary *result, NSError *error) { 

            if (!error) { 
             self.matchCenterArray = [result objectForKey:@"Top 3"]; 

             dispatch_async(dispatch_get_main_queue(), ^{ 
              [_matchCenter reloadData]; 
             }); 

             NSLog(@"Test Result: '%@'", result); 
            } 
           }]; 
} 




- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [self.matchCenterArray count]; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Initialize cell 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (!cell) { 
     // if no cell could be dequeued create a new one 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
    } 

    // populate dictionary with results 
    NSDictionary *matchCenterDictionary= [self.matchCenterArray objectAtIndex:indexPath.row]; 

    // title of the item 
    cell.textLabel.text = [matchCenterDictionary objectForKey:@"Title"]; 
    // price of the item 
    cell.detailTextLabel.text = [matchCenterDictionary objectForKey:@"Price"]; 



    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL [*matchCenterDictionary objectForKey:@"Image URL"]]]; 
    [[cell imageView] setImage:[UIImage imageWithData:imageData]]; 




    return cell; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 


/* 
#pragma mark - Navigation 


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    // Get the new view controller using [segue destinationViewController]. 
    // Pass the selected object to the new view controller. 
} 
*/ 

@end 

JSON:

{ 
    "Top 3" =  (
       { 
      "Image URL" = "http://thumbs1.ebaystatic.com/m/m43q-_-W_kKrSuwWbIO7msg/140.jpg"; 
      "Item URL" = "http://www.ebay.com/itm/New-AT-T-Moto-X-XT1058-Android-Smartphone-Black-16GB-/141304035108?pt=Cell_Phones"; 
      Price = "289.95"; 
      Title = "New AT&T Moto X XT1058 Android Smartphone Black 16GB"; 
     }, 
       { 
      "Image URL" = "http://thumbs2.ebaystatic.com/m/mP5Gx55JuDEVZlmFodzuUow/140.jpg"; 
      "Item URL" = "http://www.ebay.com/itm/New-AT-T-Moto-X-XT1058-Android-Smartphone-White-16GB-/141302889485?pt=Cell_Phones"; 
      Price = "289.95"; 
      Title = "New AT&T Moto X XT1058 Android Smartphone White 16GB"; 
     }, 
       { 
      "Image URL" = "http://thumbs2.ebaystatic.com/m/mDMiorn4BLSRBcU1EpbFPaA/140.jpg"; 
      "Item URL" = "http://www.ebay.com/itm/New-Motorola-Moto-X-16GB-White-10MP-AT-T-Branded-Unlocked-Android-Smartphone-/131194435025?pt=Cell_Phones"; 
      Price = "339.99"; 
      Title = "New Motorola Moto X 16GB White 10MP AT&T Branded Unlocked Android Smartphone"; 
     } 
    ); 
} 

回答

2

此代碼:

[NSURL [*matchCenterDictionary objectForKey:@"Image URL"]] 

看起來是罪魁禍首,我...我會嘗試:

[NSURL URLWithString:[matchCenterDictionary objectForKey:@"Image URL"]] 

這並不包括你提到的異步網絡調用...爲此,我建議使用Parse提供的PFImageView而不是UITableViewCell中內置的imageView。但有幾種方法可以完成這項任務...