gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:49:47 25 4
gpt4 key购买 nike

我正在尝试在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com