2014-01-15 18 views
0

我必須顯示一些發佈到UITableView的博客。當我檢索了所有的Web服務數據。 這是我的示例代碼。我如何顯示圖像在ios中使用asyncronous概念的uitableviewcells

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
[UIApplication sharedApplication].networkActivityIndicatorVisible=YES; 
NSError *err; 
NSString *strResponse=[[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding]; 
NSLog(@"response is %@",strResponse); 
dit=[NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:&err]; 

NSArray *arrResults = [dit valueForKey:@"classifieds_mst"]; 

listOfObjects = [NSMutableArray array]; 

for(dictRes in arrResults) 

{ 

Attributes *at = [[Attributes alloc]init]; 
at.classimage=[dictRes valueForKey:@"image_name"]; 
[listOfObjects addObject:at]; 

} 

[tableView reloadData]; 

} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *[email protected]"cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; 
if (cell == nil) 
{ 
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"identifier"]; 
} 
classifiedimage=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 300, 80)]; 
[cell.contentView addSubview:classifiedimage]; 
Attributes *att = [listOfObjects objectAtIndex:indexPath.row]; 
NSString *str; 
str=att.classimage; 
classifiedimage.image = [UIImage imageNamed:str]; 
return cell; 

} 

我的要求是我想使用JSON uitableviewcells顯示圖像parser.I上面寫的code.But圖像沒有在uitableviewcells.I顯示要使用使用Asyncronous concept.Please在uitableviewcells顯示圖像給我任何想法。我對編程新手感謝。提前感謝。 這是我的json數據。

{"classifieds_mst":[{"classified_id":83,image_name":"1389006378_butterfly.jpeg"}, 
{"classified_id":82,"image_name":"ttt.jpj"},{"classified_id":83,」image_name":"ttttt.jpj」} 
+0

是你的圖片是在本地還是在服務器 – Retro

+0

@Retro,謝謝您響應。圖片在服務器 – user2930730

回答

0

imageNamed用於從本地包(在應用程序)中加載圖像。

imageWithData:dataWithURL用於加載來自url的圖片。

+0

謝謝你的回覆。但我無法理解你說了什麼,請告訴我一些精心設計。或者在使用異步任務的情況下,我的代碼中是否有任何修改?爲什麼因爲圖像不顯示在uitablevewcell中。 – user2930730

0

通過引用您的JSON數據,它只包含image_name。所以首先,你需要構建實際的圖像url。 (複製粘貼在瀏覽器中的實際URL以確保圖像在服務器中可用)。

一旦你有了圖像url,那麼你可以在你的單元格中使用AsyncImageView而不是UIImageView。它將爲您處理異步事件,因爲您只需設置圖像url。這就是要走的路。

還有一個建議是重新檢查你cellForRowAtIndexPath,因爲代碼總是創建imageView並在每次創建單元格時添加到單元格中。當你重用單元時,這不是必需的。您只需要在分配單元格時創建一次圖像視圖,然後爲imageview分配一個標記,然後根據此標記獲取imageview,然後使用它。僅供參考,我發佈了一個示例代碼:

if(cell == nil){ 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"identifier"]; 
    AsyncImageView *imgView= [[[AsyncImageView alloc]init]autorelease]; 
    imgView.tag = 1000; 
    imgView.contentMode = UIViewContentModeScaleAspectFill; 
    [cell.contentView addSubview:imgView]; 
} 
AsyncImageView *imgView= (AsyncImageView*)[cell.contentView viewWithTag:1000]; 
contactImgView.frame = CGRectMake(0, 0, 300, 80); 

//imgUrl is the actual url of image that is available in datasource 
[contactImgView setImageURL:imgUrl]; 

希望它很清楚。

+0

感謝您的回覆。但是我的json data.so中有很多圖片,所以這些圖片被附加在MY UITableviewcells上。所以有可能這行代碼。[contactImgView setImageURL:imgUrl]; – user2930730

+0

是可能的。但是,如果您不想在應用程序中使用任何第三方控件,則需要異步下載圖像並分別設置圖像。 – HRM

0

您的圖像名稱追加域名:(IMAGE_NAME「:」 1389006378_butterfly.jpeg),這樣就可以得到被存儲在服務器上的圖像的URL,現在你可以使用下面的代碼:

[cell.yourImageView setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:yourImageNameHere]]]]; 
0

,如果您使用的是AFNetworking然後用它來下載你的形象好像

NSArray *keys = @[@"UserID", ]; 
    NSArray *objects = @[@(userId)]; 

    NSDictionary *parameter = [NSDictionary dictionaryWithObjects:objects forKeys:keys]; 

    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL: 
           [NSURL URLWithString:BaseURLString]]; 
    [httpClient setParameterEncoding:AFJSONParameterEncoding]; 
    [httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]]; 

    NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" 
                  path:@"UserService.svc/GetUserInfo" 
                 parameters:parameter]; 
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 
    [httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]]; 
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 

     NSError* error = nil; 
     id jsonObject = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:&error]; 
     } 
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      [SVProgressHUD dismiss]; 
     }); 
    }]; 

    [operation start]; 

和圖像使用本

[cell.userImage setImageWithURLRequest:[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:userBasicInfo.imageUrl]] 
            placeholderImage:[UIImage imageNamed:@"facebook-no-user.png"] 
              success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image){ 
               weakCell.userImage.image = image; 
               [weakCell setNeedsLayout]; 

               } completion:^(BOOL success, NSError *error) { 
                NSLog(@"%@",[error localizedDescription]); 
               }]; 

              } 
              failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){ 
              }]; 
0

您爲您的單元創建UITableViewCell的一個子類(可以稱爲blogTableViewCell)並在表中使用它。

初始化blogTableViewCell與JSON數據(你可以有一個NSDictionary存儲JSON資訊)

在blogTableViewCell創建加載另一個線程(因此主線程可以繼續它的課程)圖像數據的功能,一旦圖像加載,更新單元上的的UIImageView來顯示它(這個應該在主線程中完成,因爲它是一個UI更新)

0
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
    } 

    // Configure the cell... 
    //cell.textLabel.text = [[resultArray objectAtIndex:indexPath.row] valueForKey:@"name"]; 
    cell.detailTextLabel.text = [[resultArray objectAtIndex:indexPath.row] valueForKey:@"designation"]; 
    cell.imageView.image=[UIImage imageNamed:[[resultArray objectAtIndex:indexPath.row] valueForKey:@"image"]] ; 

    //[cell.imageView setImageWithURL:[NSURL URLWithString:[[resultArray objectAtIndex:indexPath.row] objectForKey:@"image"]]]; 
    // NSURL *url = [NSURL URLWithString:[resultArray objectAtIndex:indexPath .row]]; 
    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[resultArray objectAtIndex:indexPath.row] valueForKey:@"image"] ]]; 
    cell.imageView.image = [UIImage imageWithData:imageData]; 



    return cell; 
}