1

我想從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不是。幫助深表感謝!

回答

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來獲得所需的索引。


第二個是使用兩個數組。想象一下,您會看到一張名爲 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的數字相同。


我明白這是所有大量的信息,特別是如果你的新目標 - 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手機編程世界的好開端。

祝你好運!

+0

爲了詳細說明如何使用放置在URL上的圖片,我編輯了答案,以便您可以找到解釋的答案。 – Joetjah 2011-05-10 09:26:15