1

我想用 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:]: index 1 beyond bounds [0 .. 0]”

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

4

1 回答 1

4

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

您的代码目前不是最好的。您对字典的理解是错误的,可能会将它们与数组混淆,所以我最好的猜测是您对 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获取所需的索引。


第二个是使用两个数组。想象一下,您得到一个以imageOnetext命名的图像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的新手。有一个教程可以为您提供有关如何使用数组、字典和表视图的大量信息。作为奖励,您将对 XML 解析有所了解。 我建议您阅读该教程并做所有事情并阅读它所说的所有内容。这应该会给你一个进入 iPhone 编程世界的良好开端。

祝你好运!

于 2011-05-10T09:20:41.170 回答