我试图在UITableViewCell中显示图像,但图像不能使用原型单元显示。
我的代码:
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