user19634
user19634

Reputation: 15

Image not displayed in TableView in JSON parsing in iOS

I am trying to show the image in UITableViewCell but image not displaying using prototype cell.

My code:

MTViewController.h

#import "MTViewController.h"


#import "MTTableViewCell.h"

@interface MTViewController ()

{
    NSMutableArray *myObject;

    // A dictionary object

    NSDictionary *dict;

    // Define keys

    NSString  *Image_Id;

    NSString *Sub_Cat_Id;

    NSString *Image_Path;
}

@end

@implementation MTViewController

static NSString *CellIdentifier = @"CellIdentifier";


- (void)viewDidLoad {
    // Define keys

    Image_Id = @"Image_Id";

    Sub_Cat_Id = @"Sub_Cat_Id";

    Image_Path = @"Image_Path";

    // Create array to hold dictionaries

    myObject = [[NSMutableArray alloc] init];

    NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.codeappsolutions.in/ANDROID/millenium_php/php/get_subcat_img.php"]];

    id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];

    // values in foreach loop

    for (NSDictionary *dataDict in jsonObjects) {

        NSString *strImageId = [dataDict objectForKey:@"Image_Id"];

        NSString *strSubCatId = [dataDict objectForKey:@"Sub_Cat_Id"];

        NSString *strImagPath = [dataDict objectForKey:@"Image_Path"];

        dict = [NSDictionary dictionaryWithObjectsAndKeys:strImageId, Image_Id,strSubCatId, Sub_Cat_Id,strImagPath, Image_Path,nil];
        [myObject addObject:dict];

    }

    [super viewDidLoad];

    }

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


#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return myObject.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MTTableViewCell *cell = (MTTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    NSDictionary *tmpDict = [myObject objectAtIndex:indexPath.row];

    NSURL *url = [NSURL URLWithString:[tmpDict objectForKey:Image_Path]];

    NSData *data = [NSData dataWithContentsOfURL:url];

    UIImage *img = [[UIImage alloc] initWithData:data];
    [cell.mainLabel setImage:img];

    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}

@end

Upvotes: 1

Views: 376

Answers (1)

Parth Bhadaja
Parth Bhadaja

Reputation: 416

the issue is :

your Image URL is : EC323A308FA.jpg

but Image URL Should : http://example/EC323A308FA.jpg

So Your Url Is Not Valid.

Upvotes: 3

Related Questions