سؤال

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 !

لا يوجد حل صحيح

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top