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

时间:2011-05-10 07:47:06

标签: iphone objective-c xcode uitableview didselectrowatindexpath

我想用xml列表中的title和imageurl填充tablecell。 我设法将标题(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不是。非常感谢帮助!

1 个答案:

答案 0 :(得分:4)

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

您的代码目前不是最好的。你对Dictionaries的想法是错误的并且可能会将它们与数组混淆,所以我最好的猜测是你编写Objective-c的新手。

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

下面我要做两件事:

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

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

<小时/> 第一个是使用NSDictionaries数组。您将使用如下代码:

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来获取所需的索引。

<小时/> 第二个是使用两个数组。想象一下,您获得了一个名为imageOne的图片,文字为imageName。然后你应该使用:

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的新手。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. 我建议你完成这个教程并做所有事情并阅读它所说的一切。这应该会让你在iPhone的编程世界中有一个良好的开端。

祝你好运!