我想從XML列表中填充標題和imageurl的表格。 我設法將標題(NSMutableDictonary *節)和imageURL(NSMutableDictonary * sectionsImg)分別存儲到2個NSMutableDictionary中。幫助排序是URL圖像的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超出界限s [0..0]'
任何想法爲什麼?該邏輯應該是相同的XML標題和URL可以檢索和顯示。標題是可檢索的,但imageURL不是。幫助深表感謝!
爲了詳細說明如何使用放置在URL上的圖片,我編輯了答案,以便您可以找到解釋的答案。 – Joetjah 2011-05-10 09:26:15