1

xml list에서 title 및 imageurl로 tablecell을 채우고 싶습니다. 제목 (NSMutableDictonary * sections)과 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"]; 

를 선언하지만 난 이미지 URL

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라는 사실을 제외하고는.

코드가 현재 적합하지 않습니다. 당신이 사전에 잘못이 있다는 생각을하게하고 배열과 혼동을 일으킬 수 있습니다. 따라서 가장 좋은 추측은 객관적인 프로그래밍에 익숙하지 않은 것입니다.

내가 잘못하지 않으면 두 가지 목록이 있습니다. 첫 번째 목록은 이름 목록이고 두 번째 목록은 해당 이름에 해당하는 이미지입니다. 첫째, 내가 당신에게 당신의 문제를 해결하는 방법에 대한 두 가지 방법을 제공하고 있습니다

  • : 내가 두 가지 일을 할거야 아래

    . 샘플 코드가 포함되어 있으며 이에 대한 간단한 설명을 제공합니다. 내가 설명하는 부분을 이해하지 못하는 가능성이 있습니다. 이 경우, 당신은해야합니다;

  • 아래에서 설명한 두 가지 해결 방법에 대한 링크를 확인하십시오. 이 튜토리얼에서는 배열, 사전, 표 및 보너스로 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에서 해당 색인을 사용할 수도 있습니다. 이 경우 대신 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. 을 나는 당신이 튜토리얼을 통해 걸어 모든 일을하고 그것이 말하는 모든 것을 읽으십시오. 이것은 당신에게 아이폰에서 프로그래밍의 세계로 좋은 시작을 제공해야합니다.

행운을 빈다.

+0

그리고 URL에 배치 된 이미지를 사용하여 정교하게 설명 했으므로 설명 된 내용을 찾을 수 있도록 답변을 편집했습니다. – Joetjah