2011-01-31 4 views
0

하나의 NSMutableDictionary가 있으며 사용자 세부 정보가 들어 있습니다. 구문 분석을 사용하고 MutableDictionary에서 데이터를 가져 왔습니다.iPhone에서 NSMutableArray에 NSDictionary를 만드는 방법은 무엇입니까?

feedDictionary이고, {

"city = New York", 
"phone = 111111", 
"email = [email protected]", 
"year = 1986", 
"degree = Undergraduate" 

}

I는 FeedArray으로 모든 값을 첨가하고 난가 상기 데이터를 표시 한 FeedArray.Because 대한 두 사전을 생성 할 섹션 테이블보기. 테이블 섹션 데이터는 섹션 1 - [도시, 전화, 이메일] 및 섹션 -2 [연도, 학위]입니다. 데이터의 누군가가 nil이기 때문에, 그래서 그 섹션에서 특정 행을 제거해서는 안된다. 그래서 데이터가 0인지 아닌지 여부를 확인하고 싶습니다.

편집 :

내가 FeedArray에 추가하기 전에, 난 문자열이 전무인지 아닌지 확인합니다. 문자열이 nil이면 Array를 추가하지 않아야하며 데이터가 nil이 아닌 경우 FeedArray에만 추가해야합니다.

[self isEmpty:[feedDictionary valueForKey:@"city"]]?:[feedArray addObject:[feedDictionary valueForKey:@"city"]]; //section1 

[self isEmpty:[feedDictionary valueForKey:@"phone"]]?:[feedArray addObject:[feedDictionary valueForKey:@"phone"]]; //section1 

[self isEmpty:[feedDictionary valueForKey:@"email"]]?:[feedArray addObject:[feedDictionary valueForKey:@"email"]]; //section1 

[self isEmpty:[feedDictionary valueForKey:@"year"]]?:[feedArray addObject:[feedDictionary valueForKey:@"year"]]; //section2 

[self isEmpty:[feedDictionary valueForKey:@"degree"]]?:[feedArray addObject:[feedDictionary valueForKey:@"degree"]]; //section2 

-(BOOL) isEmpty :(NSString*)str{ 
     if(str == nil || [[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0) 
    return YES; 
return NO; 

}

예상 출력은, 내 배열은 5

(

section1{ 
"city = New York", 
"phone = 111111", 
"email = [email protected]", 
} 
section2 
{ 

"year = 1986", 
"degree = Undergraduate"e" 
} 

) 그래서 내가 변경 가능한 배열의 사전을 만드는 방법. 그러니 제발 나를 인도 해주세요.

감사합니다.

답변

2
NSArray *section0Keys = [NSArray arrayWithObjects:@"city", @"phone", @"email", nil]; 
NSMutableArray *section0 = [[[feedDictionary objectsForKeys:section0Keys notFoundMarker:[NSNull null]] mutableCopy] autorelease]; 
if ([section0 indexOfObject:[NSNull null]] != NSNotFound) { 
    isValid = NO; 
} 
// you don't need this if you don't care for invalid arrays. 
[section0 removeObjectsInArray:[NSArray arrayWithObject:[NSNull null]]]; 

NSArray *section1Keys = [NSArray arrayWithObjects:@"year", @"degree", nil]; 
NSMutableArray *section1 = [[[feedDictionary objectsForKeys:section1Keys notFoundMarker:[NSNull null]] mutableCopy] autorelease]; 
if ([section1 indexOfObject:[NSNull null]] != NSNotFound) { 
    isValid = NO; 
} 
[section1 removeObjectsInArray:[NSArray arrayWithObject:[NSNull null]]]; 

self.dataArray = [NSArray arrayWithObjects: 
section0, 
section1, 
nil]; 

당신의 jQuery과 데이터 소스 방법 :

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return [self.dataArray count]; 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [[self.dataArray objectAtIndex:section] count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    /.../ 
    cell.textLabel.text = [[self.dataArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; 
    return cell; 
} 

편집 : 내가 편집 한 질문을 오해 것 같아요. 코드가 첫눈에 잘 보인다.

+0

답장을 보내 주셔서 감사합니다. 그러나 내 업데이트 된 질문을보고 도와주세요. 감사. – Pugal

+0

@Pugal Devan 내 편집을 참조하십시오. –

+0

당신은 나의 것보다주의 스팬 방식이 좋습니다. 이게 내가 그랬던 것과 같은 질문 이었어 ... 뭐라고? 그리고 나서 나는 떠났다. 엄지 척. –

관련 문제