Ios 使用json加载UITableView图像未正确加载
我用它来通过JSON解析显示一些标签和图像到tableView,但是当滚动tableView图像时,图像在第一次启动时没有出现,我的意思是没有按顺序出现。请帮我解决这个问题Ios 使用json加载UITableView图像未正确加载,ios,objective-c,iphone,json,uitableview,Ios,Objective C,Iphone,Json,Uitableview,我用它来通过JSON解析显示一些标签和图像到tableView,但是当滚动tableView图像时,图像在第一次启动时没有出现,我的意思是没有按顺序出现。请帮我解决这个问题 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { self.customCellClass = [tableView dequeueReusableCe
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
self.customCellClass = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (self.customCellClass == nil)
{
self.customCellClass = [[CellCustom alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
self.customCellClass.nameLabel.text = [[arrayData objectAtIndex:indexPath.row] objectForKey:@"name"]; // label
self.customCellClass.cityLabel.text = [[arrayData objectAtIndex:indexPath.row] objectForKey:@"region"]; // label
NSString * stripped = [[[arrayData objectAtIndex:indexPath.row] objectForKey:@"summary"] stripHtml]; //label
self.customCellClass.detailLabel.text = stripped;
self.customCellClass.mainImage.image = nil;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void) {
NSData *data0 = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[[arrayData objectAtIndex:indexPath.row]objectForKey:@"images"]objectForKey:@"logo"]]];
UIImage *image = [UIImage imageWithData:data0];
dispatch_sync(dispatch_get_main_queue(), ^(void) {
self.customCellClass.mainImage.image = image;
});
});
return self.customCellClass;
}
替换
UITableView的cellforrowatinexpath
方法中的以下代码:
//Check if cell has image or not
if(!self.customCellClass.mainImage.image)
{
dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(q, ^{
NSData *data0 = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[[arrayData objectAtIndex:indexPath.row]objectForKey:@"images"]objectForKey:@"logo"]]];
UIImage *image = [UIImage imageWithData:data0];
//Get main queue
dispatch_async(dispatch_get_main_queue(), ^{
/* This is the main thread again, where we set the tableView's image to be what we just fetched. */
self.customCellClass.mainImage.image = image;
});
});
}
好的解决方案是,如果您正在使用,则使用UIImageView+AFNetworking
category
NSURL *imageURL = [NSURL URLWithString:[[[arrayData objectAtIndex:indexPath.row]objectForKey:@"images"]objectForKey:@"logo"]]
[self.customCellClass.mainImage setImageWithURL:imageURL placeholderImage:nil];
每次单元格加载和下载同一图像时,您都将image=nil赋值
您可以使用以下类无缝地执行此操作
AsyncImageView.h
#import <UIKit/UIKit.h>
@interface AsyncImageView : UIView {
NSURLConnection *connection;
NSMutableData *data;
NSString *urlString; // key for image cache dictionary
}
-(void)loadImageFromURL:(NSURL*)url;
-(void)loadBackgroundImage:(UIImage *)image;
-(UIImage*)loadImageFromURLForGetIamge:(NSURL*)url;
-(BOOL)getCachedImageWithUrl:(NSString*)url;
@end
使用dispatch\u async
从远程加载图像,这意味着要在tableview单元格中显示的图像是异步加载的。您使用一个实例变量来记录当前单元格,这导致了您遇到的问题
图像加载完成后(可能需要几分钟),它希望显示在单元格中(用此代码编写)
结果发现,self.customCellClass
指向错误的单元格(加载图像期间多次调用了-(UITableViewCell*)cellForRowAtIndexPath:(NSIndexPath*)indexPath
,每次调用都会将self.customCellClass
指向其他单元格)
所以,图像的顺序是错误的
试试这个:
使用局部变量保持单元格get fromdequeueReusableCellWithIdentifier
。像这样:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
CellCustom *customCellClass = (CellCustom *)[tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (customCellClass == nil)
{
customCellClass = [[CellCustom alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
customCellClass.nameLabel.text = [[arrayData objectAtIndex:indexPath.row] objectForKey:@"name"]; // label
customCellClass.cityLabel.text = [[arrayData objectAtIndex:indexPath.row] objectForKey:@"region"]; // label
NSString * stripped = [[[arrayData objectAtIndex:indexPath.row] objectForKey:@"summary"] stripHtml]; //label
customCellClass.detailLabel.text = stripped;
customCellClass.mainImage.image = nil;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void) {
NSData *data0 = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[[arrayData objectAtIndex:indexPath.row]objectForKey:@"images"]objectForKey:@"logo"]]];
UIImage *image = [UIImage imageWithData:data0];
dispatch_sync(dispatch_get_main_queue(), ^(void) {
customCellClass.mainImage.image = image;
});
});
return customCellClass;
}
此外,请看一下-(id)dequeueReusableCellWithIdentifier:(NSString*)identifier
和-(id)dequeueReusableCellWithIdentifier:(NSString*)indexath的标识符:(nsindexath*)之间的区别indexPath
当我滚动tableview图像后,应用程序首次启动图像为空,但顺序不正确我没有使用任何afnetworking框架如果我使用afnetworking框架,这很好,但在您需要进行http连接的整个项目中,可以使用afnetworking。什么是“XTest/XTest.h类”在将AFNetworking文件夹拖动到项目中后,未发现它在项目中拖动AFNetworking和UIKit+AFNetworking文件夹并导入#导入“AFNetworking.h”#导入“UIImageView+AFNetworking.h”你能告诉我我必须改变什么吗?是的,我试过了,但它对我不起作用,所以我使用了AFNetworking framework,现在一切对mesorry都很好,我没有试过给我一次机会,它对我不起作用图像以非常糟糕的方式出现,它们正在跳转到另一个位置,我用AFNetworking framework找到了任何解决方案,谢谢为了帮助我为什么不试试SDWebImage?@MaheshM
dispatch_sync(dispatch_get_main_queue(), ^(void) {
self.customCellClass.mainImage.image = image;
});
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
CellCustom *customCellClass = (CellCustom *)[tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (customCellClass == nil)
{
customCellClass = [[CellCustom alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
customCellClass.nameLabel.text = [[arrayData objectAtIndex:indexPath.row] objectForKey:@"name"]; // label
customCellClass.cityLabel.text = [[arrayData objectAtIndex:indexPath.row] objectForKey:@"region"]; // label
NSString * stripped = [[[arrayData objectAtIndex:indexPath.row] objectForKey:@"summary"] stripHtml]; //label
customCellClass.detailLabel.text = stripped;
customCellClass.mainImage.image = nil;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void) {
NSData *data0 = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[[arrayData objectAtIndex:indexPath.row]objectForKey:@"images"]objectForKey:@"logo"]]];
UIImage *image = [UIImage imageWithData:data0];
dispatch_sync(dispatch_get_main_queue(), ^(void) {
customCellClass.mainImage.image = image;
});
});
return customCellClass;
}