简体   繁体   English

从iPhone中的NSMutableArray填充数据

[英]Populating Data from NSMutableArray in iPhone

I have three arrays and I have copied all these arrays into a single array. 我有三个数组,并将所有这些数组复制到一个数组中。 All these three arrays are arrays of dictionaries.All arrays has a field called picture, but that pictures is coming from different sources- URL in one array, data in other and files in the third one. 这三个数组都是字典数组。所有数组都有一个称为picture的字段,但这些图片来自不同的来源-一个数组中的URL,另一个数组中的数据,以及第三个数组中的文件。

Say, Array1 has dictionaries with a key - picture and its loaded from NSURL . 比方说,Array1的字典包含一个关键图片,并且它是从NSURL加载的。 Similarly, Array2 and Array3 has dictionaries with same key name - picture and loaded from ContentofFiles and NSData . 同样,Array2和Array3的字典具有相同的键名ContentofFiles ,并从ContentofFilesNSData加载。

Now, I want to populate tableview, of course,m having Custom UITableViewCell , it has image view as its content view. 现在,我想填充tableview,当然要有Custom UITableViewCell ,它具有图像视图作为其内容视图。 To load that image, what should I do. 要加载该图像,我该怎么办。

I was doing this thing.. 我在做这个事..

NSURL *url  = [NSURL URLWithString:[[contactList objectAtIndex:indexPath.row] objectForKey:@"picture"]];
cell.contactImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];

But, this will crash if cell.contactImageView.image don't receive image from NSURL.So, what should I do? 但是,如果cell.contactImageView.image没有收到来自NSURL的图像,这将会崩溃。那我该怎么办? Any help, will be appreciated 任何帮助将不胜感激

But, 但,

all you is to check if the received image is null and if it is, then set a template photo image called no photo like a photo on facebook when no profile picture is selected 您要做的就是检查接收到的图像是否为空,如果没有,则设置一个模板照片图像,称为无照片,就像在Facebook上的照片一样。

    UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
    if (img)
       cell.contactImageView.image = img;
    else
       cell.contactImageView.image = [UIImage imageNamed:@"no_photo.png"];

If these images are retrieved from the net, i would suggest not using [NSData dataWithContentsOfURL:] . 如果这些图像是从网上检索的,我建议不要使用[NSData dataWithContentsOfURL:]

You should be using a non blocking method, that loads images asynchronously. 您应该使用非阻塞方法,该方法异步加载图像。 Using this method on numerous rows in a table will cause performance issues. 在表中的许多行上使用此方法将导致性能问题。

Here's my recommendation , use the SDWebImage Library . 这是我的建议,请使用SDWebImage库 Easy to use, and even easier to install. 易于使用,甚至更易于安装。

Once you add the library to your project, simply #import the UIImageView+WebCache.h class usage example is below. 将库添加到项目后,只需在下面UIImageView+WebCache.h #import UIImageView+WebCache.h类的用法示例。

 [cell.contactImageView.image setImageWithURL:[NSURL URLWithString:[[contactList objectAtIndex:indexPath.row] objectForKey:@"picture"]]
               placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

您可以在字典中再添加一个键,该键将指定从何处拍照,如果您要提供来自其他来源的图像,则可以向cell.contactImageView提供图像。

Thanks a lot buddies for your Quick reply,especially @skram, due to your suggestion,my tableview performance has increased much. 非常感谢您的快速回复,特别是@skram,由于您的建议,我的表视图性能有了很大提高。 the Question, that I have asked,the better answer that I have thought of is using iskindofClass . 我问过的问题,我想到的更好的答案是使用iskindofClass If image is coming from any class, on a condition ,we can check from where that image was coming and populate our image accordingly. 如果图像来自任何类别,在一定条件下,我们可以检查该图像来自何处,并相应地填充我们的图像。

if ([[[contactList objectAtIndex:indexPath.row] objectForKey:@"picture"] isKindOfClass:[UIImage class]]) 
    {
        cell.contactImageView.image = [[contactList objectAtIndex:indexPath.row] objectForKey:@"picture"];
    }
    else if ([[[contactList objectAtIndex:indexPath.row] objectForKey:@"picture"] isKindOfClass:[NSString class]]) 
    {
        cell.contactImageView.image = [[contactList objectAtIndex:indexPath.row] objectForKey:@"picture"];
    }
    else if([[[contactList objectAtIndex:indexPath.row] objectForKey:@"picture"] isKindOfClass:[NSURL class]])
    {
        [cell.contactImageView setImageWithURL:[NSURL URLWithString:[[contactList objectAtIndex:indexPath.row] objectForKey:@"picture"]]placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
    }

Now, I'm able to populate the tableview properly.Once again thanx to scram . 现在,我能够再次填充的tableview properly.Once感谢名单,以scram

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM