开发者

help on sorting NSMutableDictionary strings that are URL images

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 er开发者_如何学Pythonror : 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 !


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜