2012-11-06 120 views
1

我会解释我的问题,希望能找到解决办法。我正在寻找几周来解决这个问题,但是找不到如何去做,不想使用外部库或稀有类。TableView与异步加载远程图像

说明:

我有一个TableView中从XML自动获得的信息,该XML我的全部被解析并存储在MutableArray的信息。

该数组解析我所有的XML标签,它工作正常。

要读取一个标签我做我的解析器如下:

//要使用这个标签标题:

[self.parseResults objectAtIndex:indexPath.row] objectForKey:@ “name_category_ads”];

//要使用该图像标签:

[[self.parseResults objectAtIndex:indexPath.row] objectForKey:@ “image1_category_ads”];

问题:

的TableView中文本正确加载我的阵列(标题),但我不能拥有它加载从我的阵列中的图像的tableview:

我测试我的代码以静态URL图像(一个图像在网站上)和异步加载,如果它的工作,但不工作,当我尝试从我的数组,它具有不同的图像网址。

//图像URL静态

NSString *[email protected]"http://www.principadoasturias.net/2011/pablo/03_php/archivos/iconos/jpg.jpg"; 

的代码为我的表中的实施例是如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *CellIdentifier = @"Cell"; 

//My custom cell 

Cell_Category_Iphone *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (!cell) { 
    cell = [[Cell_Category_Iphone alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 

//Image Static URL 
NSString *[email protected]"http://www.principadoasturias.net/2011/pablo/03_php/archivos/iconos/jpg.jpg"; 

//Assign the title to my cell, using my array already loaded and parsed. 

cell.titleCategoryCell.text=[[self.parseResults objectAtIndex:indexPath.row] objectForKey:@"name_category_ads"]; 

//Asynchronous loading of image 

if (!cell.imageCategoryCell.image) { 

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{ 

NSData *data =[NSData dataWithContentsOfURL:[NSURL URLWithString:imagenURL]]; 

dispatch_sync(dispatch_get_main_queue(), ^{ 
     UIImage *thumbnail = [UIImage imageWithData:data]; 
     cell.imageCategoryCell.image=thumbnail; 
     [self.tableView beginUpdates]; 
     [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone]; 
     [self.tableView endUpdates]; 
    }); 
}); 
} 

return cell; 
} 

四舍五入了问题,其中包括:

的代码加载正确的图像asicronica,但在URL中的单个图像,但我不能让它加载我的数组中的图像。

//我的阵列图像

[[self.parseResults objectAtIndex: indexPath.row] objectForKey: @ "image1_category_ads"]; 

你能帮助我吗?然后分享给大家的代码。 非常感谢

问候当你正在使用的AFNetworking library有一个非常好的实施里卡多

回答

2

。它是UIImageView的一个扩展,它为您处理异步加载。你只需要设置URL,这是一个单线程。看看UIImageView+AFNetworking at GitHub。如果您不想使用AFNetworking库,您可能需要have a look at this sample。这是非常基本的,也是一样的,但只有一个简单的课程。

+0

嗨,感谢您的回复。 这两个都不适合我,第一个非常复杂,第二个没有得到它的工作。 任何其他帮助? 谢谢 –

+0

你好,实现解决!非常感谢你 –

0

设法解决问题。我所做的就是验证并消除图像阵列,空白区域和换行符,并且完美运行。 问候 里卡多