2013-03-19 6 views
1

UITableView를 채우는 단일 차원 배열을 구문 분석하는 iOS 응용 프로그램을 만들었습니다. 두 개의 항목을 배열의 "Name"파일과 "URL" XML에서 파일의. 그러나 테이블보기에서 이름과 URL이 채워졌습니다. 셀 텍스트로 이름을 표시하고 셀 세부 정보 텍스트로 URL을 표시하려고합니다. 어떤 도움이 필요합니까?Objective C에서 2 차원 배열을 구문 분석

(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{ 
//NSLog(@"%@",string); 
if(count){ 
    listset1=[[NSMutableArray alloc]initWithCapacity:20]; 
    count=0; 
} 


if ([currentElementValue isEqualToString:@"Name"]) { 
    [Name appendString:string]; 
    [listset1 addObject:[NSString stringWithFormat:@"%@",Name]]; 
     // NSLog(@"%@",listset1); 
} 

if ([currentElementValue isEqualToString:@"URL"]) { 
    [URL appendString:string]; 
    [listset1 addObject:[NSString stringWithFormat:@"%@",URL]]; 
    //NSLog(@"%@",URL); 

} 

Listset1은 구문 분석되는 배열입니다.

+0

에서 다음 메소드를 구현 URL에 해당합니다. 그 사전에서 당신은 이름과 URL을 가져 와서 cellFororwoAtindexpath에 텍스트를 설정해야합니다. –

+0

사전 배열을 사용할 수 있습니다 :'[{ "name": ; "URL": }, { "name": ; "URL": }, ...] – nielsbot

+0

클래스 구조 내에서 쉽게 데이터를 유지 관리 할 수 ​​있습니다. –

답변

1

그것은 대물-C 언어로 가능하지 않다.

두 개의 배열을 사용하거나 "클래스" 구조를 참조하십시오.

클래스 구조를 선호합니다.

이름URL에 대한 NSString 같은 두 개의 객체로 NSObject의 클래스 형식을 만듭니다.

값을 가져 오는 동안,

sampleClass *objClass = [[sampleClass alloc] init]; 

objClass.strName = [NSString stringWithFormat:@"%@", strName]; 
objClass.strURL = [NSString stringWithFormat:@"%@", strURL]; 

[listArray addObject: objClass]; 

데이터를 표시하는 동안,

for (int i = 0; i < [listArray count]; i++) 
{ 
    objClass = [listArray objectAtIndex:i]; 

    NSLog(@" --> %@", objClass.strName); 
    NSLog(@" --> %@", objClass.strURL); 
} 

희망, 당신은 이해하게 될 것입니다.

감사합니다.

+1

"클래스 구조"는 모델이라고 더 잘 부릅니다. 또한 이것은 작은 nitpick이지만 대개 Objective-C에서는 메서드 이름의 접두사 나이 경우 속성을 대문자로 사용하지 않습니다. 따라서이 경우 모델에 'name'및 'url'속성이있는 것이 좋습니다. –

+0

@ sudorm-rf : 이봐, 난 그냥 이름과 URL에 대한 예를 말하고있다. 우리는 'strName'& 'strURL'로 취할 수 있습니다. –

+0

@ sudorm-rf : 변수에 대한 대답을 편집했습니다. –

0

당신은 다차원 배열이 possibilty하지 NSMutableArray

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{ 

     if(count){ 
      NSMutableArray* listset1=[[NSMutableArray alloc]initWithCapacity:20]; 
      count=0; 
     } 

     NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; 
     if ([currentElementValue isEqualToString:@"Name"]) { 
      [Name appendString:string]; 
      [dict setValue:[NSString stringWithFormat:@"%@",Name] forKey:@"Name"]; 
     } 

     if ([currentElementValue isEqualToString:@"URL"]) { 
      [URL appendString:string]; 
      [dict setValue:[NSString stringWithFormat:@"%@",URL] forKey:@"URL"]; 

     } 
     [listset1 addObject:dict]; 
     [dict release]; 
    } 
0

에 이름과 URL NSMutableDictionary 및 저장소를 저장합니다.

의 당신이 두 개의 서로 다른 배열을 가지고 있고 둘 사이의 의미를 보존했다고 가정합시다. 그래서 이름이 [0] [0]

지금 당신의 ViewController 당신은 측면 배열의 NSDictionories을해야

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    static NSString *CellIdentifier; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 
cell.textlabel.text=name[indexPath.row]; 
cell.detailTextLabel.text=url[indexPath.row]; 

return cell; 


}