ios - iOS 中 JSON 解析中的 TableView 中未显示图像

标签 ios objective-c json uitableview uiimageview

我正在尝试在 UITableViewCell 中显示图像,但图像未使用原型(prototype)单元格显示。

我的代码:

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

最佳答案

问题是:

您的图片网址是:EC323A308FA.jpg

但图片 URL 应为:http://example/EC323A308FA.jpg

所以你的网址无效。

关于ios - iOS 中 JSON 解析中的 TableView 中未显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32117601/

相关文章:

ios - Swift 4 - 使用 PromiseKit 调用嵌套

当回显较长的字符串时,PHP echo 无法给出输出

ruby-on-rails - 使用 respond_with 包含多个关联

ios - 具有通用解码类型的网络调用

ios - Swift - 覆盖整个屏幕的滑动菜单

iOS 7 全屏 Web 应用程序中的 JavaScript 警报损坏了吗?

iphone - 自定义 Arial-Narrow.ttf 字体集成

iphone - 如果我使用 @property(atomic,retain)NSString *myString 我会遇到问题

android - 从android中的嵌套json获取值并将其传递给listview

ios - UICollectionViewCell 的子类中的“ socket 无法连接到重复内容”