帮助对作为 URL 图像的 NSMutableDictionary 字符串进行排序

发布于 2024-11-06 15:13:51 字数 2848 浏览 0 评论 0原文

我想用 xml 列表中的标题和 imageurl 填充表格单元格。 我设法将标题(NSMutableDictonary *sections)和imageURL(NSMutableDictonary *sectionsImg)分别存储到2个NSMutableDictionary中。

/*******This is in viewDidLoad***/
Directory *allDirectory = [appDelegate.directories objectAtIndex:0];

for (allDirectory in appDelegate.directories)
{
    NSDictionary *dica = [NSDictionary dictionaryWithObject:allDirectory.dirTitle forKey:@"dirTitle"];
    NSDictionary *dico = [NSDictionary dictionaryWithObject:allDirectory.imageURL forKey:@"imageURL"];


    [dirName addObject:dica];
    [dirImage addObject:dico];
    //NSLog(@"dic of items : %@",dirImage);


}
for (allDirectory in appDelegate.directories)
{
    //retrieve the first letter from every directory title (dirTitle)
    NSString * c = [allDirectory.dirTitle substringToIndex:3];

    NSString * m = allDirectory.imageURL;



    found = NO;
    find = NO;

    for (NSString *str in [self.sections allKeys])
    {
        if ([str isEqualToString:c])
        {
            found = YES;
        }
    }
    for (NSString *stra in [self.sectionsImg allKeys])
    {
        if([stra isEqualToString:m])
        {
            find = YES;
        }
    }

    if (!found)
    {
            [self.sections setValue:[[NSMutableArray alloc]init] forKey:c ];
            [self.sectionsImg setValue:[[NSMutableArray alloc]init] forKey:m];
    }
    if (!find)
    {
        [self.sectionsImg setValue:[[NSMutableArray alloc]init] forKey:m];
    }

}

for (NSDictionary *directory in dirName)
{
    [[self.sections objectForKey:[[directory objectForKey:@"dirTitle"] substringToIndex:3]] addObject:directory];
    //NSLog(@"hehehe have : %@",sections);
}
for (NSDictionary *directoryImg in dirImage)
{
    //[[self.sectionsImg objectForKey:[[directoryImg objectForKey:@"imageURL"] substringFromIndex:0]] addObject:directoryImg];
    [[self.sectionsImg objectForKey:[directoryImg objectForKey:@"imageURL"]] addObject:directoryImg];

    //NSLog(@"HOHOHO have : %@",sectionsImg);
}

在 cellForRowAtIndexPath 上我声明了一个字典,

NSDictionary *dictionary = [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
cell.textLabel.text = [dictionary objectForKey:@"dirTitle"];

但是当我尝试为 imageURL 声明一个字典时,

NSDictionary *dictionaryImg = [[self.sectionsImg valueForKey:[[[self.sectionsImg allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];

它给了我一个错误: 由于未捕获的异常“NSRangeException”而终止应用程序,原因:“* -[NSMutableArray objectAtIndex:]:索引 1 超出范围 [0 .. 0]”

知道为什么吗?逻辑应该与可以检索并显示 xml 标题和 url 的逻辑相同。标题是可检索的,但 imageURL 则不可检索。非常感谢您的帮助!

i want to populate the tablecell with title and imageurl from xml list.
i manage to store the title (NSMutableDictonary *sections )and imageURL (NSMutableDictonary *sectionsImg) into 2 NSMutableDictionary respectively.

/*******This is in viewDidLoad***/
Directory *allDirectory = [appDelegate.directories objectAtIndex:0];

for (allDirectory in appDelegate.directories)
{
    NSDictionary *dica = [NSDictionary dictionaryWithObject:allDirectory.dirTitle forKey:@"dirTitle"];
    NSDictionary *dico = [NSDictionary dictionaryWithObject:allDirectory.imageURL forKey:@"imageURL"];


    [dirName addObject:dica];
    [dirImage addObject:dico];
    //NSLog(@"dic of items : %@",dirImage);


}
for (allDirectory in appDelegate.directories)
{
    //retrieve the first letter from every directory title (dirTitle)
    NSString * c = [allDirectory.dirTitle substringToIndex:3];

    NSString * m = allDirectory.imageURL;



    found = NO;
    find = NO;

    for (NSString *str in [self.sections allKeys])
    {
        if ([str isEqualToString:c])
        {
            found = YES;
        }
    }
    for (NSString *stra in [self.sectionsImg allKeys])
    {
        if([stra isEqualToString:m])
        {
            find = YES;
        }
    }

    if (!found)
    {
            [self.sections setValue:[[NSMutableArray alloc]init] forKey:c ];
            [self.sectionsImg setValue:[[NSMutableArray alloc]init] forKey:m];
    }
    if (!find)
    {
        [self.sectionsImg setValue:[[NSMutableArray alloc]init] forKey:m];
    }

}

for (NSDictionary *directory in dirName)
{
    [[self.sections objectForKey:[[directory objectForKey:@"dirTitle"] substringToIndex:3]] addObject:directory];
    //NSLog(@"hehehe have : %@",sections);
}
for (NSDictionary *directoryImg in dirImage)
{
    //[[self.sectionsImg objectForKey:[[directoryImg objectForKey:@"imageURL"] substringFromIndex:0]] addObject:directoryImg];
    [[self.sectionsImg objectForKey:[directoryImg objectForKey:@"imageURL"]] addObject:directoryImg];

    //NSLog(@"HOHOHO have : %@",sectionsImg);
}

And on cellForRowAtIndexPath i declare a dictionary

NSDictionary *dictionary = [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
cell.textLabel.text = [dictionary objectForKey:@"dirTitle"];

but when i tried to declare a dictionary for imageURL

NSDictionary *dictionaryImg = [[self.sectionsImg valueForKey:[[[self.sectionsImg allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];

it gives me a error :
Terminating app due to uncaught exception 'NSRangeException', reason: '* -[NSMutableArray objectAtIndex:]: index 1 beyond bounds [0 .. 0]'

any idea why? the logic is supposed to be the same where xml title and url can be retrieve and be displayed. Title is retrievable but imageURL is not. Help is deeply appreciated !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沉睡月亮 2024-11-13 15:13:51

您正在尝试对数组进行排序...除了您的数组不是数组,而是 NSDictionary 的事实。

您的代码目前不是最好的。您对字典的理解是错误的,并且可能会将它们与数组混淆,所以我最好的猜测是您对 Objective-C 编程还很陌生。

如果我没记错的话,你有两份清单。第一个列表是名称列表,第二个列表是与该名称对应的图像。

下面我将做两件事:

  • 首先,我为您提供两种解决问题的方法。它包含一个示例代码,并为您提供了一个简短的解释。您可能不理解我所描述的部分内容。在这种情况下,您应该;
  • 查看我在下面描述的两个解决方案的链接。它有一个教程,可以让您了解有关数组、字典、表以及 XML 解析的所有内容。

所以,在我看来,你可以做两件事:


The first one is using an array of NSDictionaries. You'd be using a code which looks like:

NSMutableDictionary *itemOne = [[NSMutableDictionary alloc] init];
NSMutableDictionary *itemTwo = [[NSMutableDictionary alloc] init];
NSMutableArray *listOfAll = [[NSmutableArray alloc] init];
NSString *itemOneName = [[NSString alloc] initWithFormat:@"This is picture 1"];
NSString *itemTwoName = [[NSString alloc] initWithFormat:@"This is picture 2"];
NSData *imageOneData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: @"http://myurl/mypic1.jpg"]];
NSData *imageTwoData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: @"http://myurl/mypic2.jpg"]];
UIImage *itemOneImage = [UIImage imageWithData: imageOneData];
UIImage *itemTwoImage = [UIImage imageWithData: imageTwoData];
[itemOne setObject:itemOneNameString forKey:@"Name"];
[itemOne setObject:itemOneImage forKey:@"Image"];
[itemTwo setObject:itemTwoNameString forKey:@"Name"];
[itemTwo setObject:itemTwoImage forKey:@"Image"];
[listOfAll addObject:itemOne];
[listOfAll addObject:itemTwo];

使用该数组可以填充任何内容。只需使用带有 for 循环的东西即可迭代数组。

for (int i = 0; i < [listOfAll count]; i++)
{
    NSMutableDictionary *currentItem = [[NSMutableDictionary alloc] initWithDictionary:[listOfAll objectAtIndex:i]];
    //Do something with that current item
}

您还可以在 tableView 中使用该索引。在这种情况下,您必须使用变量 section 而不是 i 来获取所需的索引。


The second one is using two arrays. Imagine you get an image named imageOne with the text imageName. Then you should use:

NSMutableArray *nameList = [[NSMutableArray alloc] init];
[nameList addObject: imageName];
NSMutableArray *imageList = [[NSMutableArray alloc] init];
[imageList addObject: imageOne];

如果您想使用这些列表中的某个项目,则只需使用相同的索引号即可。
例如:

[theTitleLabel setText:[[NSString alloc] initWithFormat:@"%@", [nameList objectAtIndex:x]]];
[theImageView setImage:[imageList objectAtIndex:x]];

确保 x 是相同的数字。


我知道这有很多信息,特别是如果您是 Objective-C 的新手。 有一个教程,为您提供了大量有关如何使用数组、字典和表视图的信息。作为奖励,您可以了解一些有关 XML 解析的知识。
我建议您完成该教程并执行所有操作并阅读其中所说的所有内容。这应该会给你一个进入 iPhone 编程世界的良好开端。

祝你好运!

You are trying to sort an array... except for the fact your array isn't an array, but a NSDictionary.

Your code isn't the best at the moment. Your getting the idea of Dictionaries wrong and may be confusing them with arrays, so my best guess is your quite new to programming into objective-c.

You have two lists of things, if I'm not mistaken. The first list is the list of names, and the second list is an image corresponding with that name.

Below I'm going to do two things:

  • Firstly, I'm giving you two ways on how to fix your problem. It has a sample code included and gives you a small explanation with it. The possibility exist you don't understand parts of what I describe. In that case, you should;
  • Check out the link I described below the two solutions. It has a tutorial which makes you understand everything about arrays, dictionaries, tables and, as a bonus, XML-parsing.

So, in my opinion, you can do two things:


The first one is using an array of NSDictionaries. You'd be using a code which looks like:

NSMutableDictionary *itemOne = [[NSMutableDictionary alloc] init];
NSMutableDictionary *itemTwo = [[NSMutableDictionary alloc] init];
NSMutableArray *listOfAll = [[NSmutableArray alloc] init];
NSString *itemOneName = [[NSString alloc] initWithFormat:@"This is picture 1"];
NSString *itemTwoName = [[NSString alloc] initWithFormat:@"This is picture 2"];
NSData *imageOneData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: @"http://myurl/mypic1.jpg"]];
NSData *imageTwoData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: @"http://myurl/mypic2.jpg"]];
UIImage *itemOneImage = [UIImage imageWithData: imageOneData];
UIImage *itemTwoImage = [UIImage imageWithData: imageTwoData];
[itemOne setObject:itemOneNameString forKey:@"Name"];
[itemOne setObject:itemOneImage forKey:@"Image"];
[itemTwo setObject:itemTwoNameString forKey:@"Name"];
[itemTwo setObject:itemTwoImage forKey:@"Image"];
[listOfAll addObject:itemOne];
[listOfAll addObject:itemTwo];

Anything can be filled using that array. Just use something with a for-loop to iterate through your array.

for (int i = 0; i < [listOfAll count]; i++)
{
    NSMutableDictionary *currentItem = [[NSMutableDictionary alloc] initWithDictionary:[listOfAll objectAtIndex:i]];
    //Do something with that current item
}

You can also use that index in your tableView. In that case, you have to use your variable section instead of i to get your desired index.


The second one is using two arrays. Imagine you get an image named imageOne with the text imageName. Then you should use:

NSMutableArray *nameList = [[NSMutableArray alloc] init];
[nameList addObject: imageName];
NSMutableArray *imageList = [[NSMutableArray alloc] init];
[imageList addObject: imageOne];

If you want to use a certain item out of those lists, you just have to use the same indexnumber.
For example:

[theTitleLabel setText:[[NSString alloc] initWithFormat:@"%@", [nameList objectAtIndex:x]]];
[theImageView setImage:[imageList objectAtIndex:x]];

Make sure the x's are the same number.


I understand this is all a lot of information, especially if you're new to Objective - C. A tutorial exists which gives you a lot of information about how to use arrays, dictionaries and table views. As a bonus, you get to know a little about XML-parsing.
I suggest you walk through that tutorial and do everything and read everything it says. This should give you a nice start into the world of programming in iPhones.

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文