2011-08-31 5 views
2

게시하기 전에 거의 모든 것을 시도했습니다 (Google, Apple Dev Doc 등). 게시하기 전에 아무 것도 놓치지 않았 으면 좋겠다.UITableView iOS 용 NSArray 데이터 소스로 복잡한 JSON

나는 이미 JSON < -> Webservice < -> iPhone 로그인을 구축했다.

NSString *responseString = [[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]autorelease]; 
NSDictionary *jsonDict = [responseString JSONValue]; 
NSString *loginResult = [jsonDict valueForKey:@"LoginResult"]; 

loginResult는 직원의 이름입니다 : 다음 코드로

예를 들어, "벵거 헤닝"

는 해당 JSON은 다음과 같습니다

{"LoginResult":"S:00000412"} 

지금은이 더 복잡한 작업을 통해 나는 방법을 찾아 낼 수 없다 :

나는 웹 서비스에서 다음 JSON을 받고 :

NSDictionary *jsonDict = [responseString JSONValue]; 
NSArray *teamStatus = [jsonDict objectForKey:@"GetTeamPresentStatusResult"]; 
[self BuildDataSource: nil :teamStatus]; 

teamStatus 지금 10 키/값 쌍을 포함

{"GetTeamPresentStatusResult"[ 
{"Firstname":"Steffen","Id":"00000456","IsPresent":"true","Lastname":"Polker"}, 
{"Firstname":"Erich","Id":"00000455","IsPresent":"true","Lastname":"Welter"},  
{"Firstname":"Titus","Id":"00000454","IsPresent":"true","Lastname":"Sommerbeck"},  {"Firstname":"Ruediger","Id":"00000453","IsPresent":"true","Lastname":"Oelmann"},{"Firstname":"Heinz","Id":"00000452","IsPresent":"true","Lastname":"Radelfs"},{"Firstname":"Franz","Id":"00000451","IsPresent":"true","Lastname":"Wippermann"},{"Firstname":"Klaus-Dieter","Id":"00000450","IsPresent":"true","Lastname":"Just"},{"Firstname":"Alan","Id":"00000412","IsPresent":"true","Lastname":"Turing"},{"Firstname":"Konrad","Id":"00000138","IsPresent":"true","Lastname":"Zuse"},{"Firstname":"Marius","Id":"00000112","IsPresent":"true","Lastname":"Sandmann"}]} 

이 처리를위한 내 코드로 시작합니다. 여기

내가 내 테이블에 대한 데이터 소스를 구축하려고하는 방법이다 :

- (void)BuildDataSource: (id) sender: (NSArray*)teamStatusData{ 

teamStatusMutableArray = [[NSMutableArray alloc]init]; 
teamStatusDataSection = [[NSArray alloc]initWithArray:teamStatusData]; 

teamStatusDictSectionOne = [NSDictionary dictionaryWithObject:teamStatusDataSection forKey:@"OrgUnits"]; 
[teamStatusMutableArray addObject:teamStatusDictSectionOne]; 

[tblView reloadData]; 
} 

내가 그것을 필요로하는지 데이터를 얻기 위해 여기에서 이동하는 방법을 알아낼 관리 할 수 ​​없습니다.

첫 번째/값이 포함 된 다른 배열과 성/값이 포함 된 다른 배열이 필요합니까? 그렇다면 어떻게해야합니까? 여기

는 I've 지금까지 도움이되지 않았다 시도 몇 줄 수 있습니다

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

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
} 

// Set up the cell... 
//First get the dictionary object 

NSDictionary *dictionary = [teamStatusMutableArray objectAtIndex:indexPath.section]; 

NSString *testString = [dictionary valueForKey:@"Firstname"]; 
NSArray *testarray = [dictionary objectForKey:@"OrgUnits"]; 

NSString *test = [testarray JSONRepresentation]; 

test = [test stringByReplacingOccurrencesOfString:@"[" withString:@""]; 
test = [test stringByReplacingOccurrencesOfString:@"]" withString:@""]; 

NSDictionary *jsonDict = [test JSONValue]; 
} 

나는 테이블에 결합 할 수있는 직원 이름과 데이터 소스를 가지고있다 필요한 것은. 거기에서 나는 추가 도움없이 내 길을 나아갈 수 있다고 생각합니다.

저는 .NET (C#) 세계에서 왔고 어쩌면 혼란 스럽습니다. 당신은 자세한 문의하시기 바랍니다 필요한 경우

은 .. 당신에게

헤닝

을 너무 많이 주셔서 감사합니다! SPtail에 대한

세부 사항 :

#import <UIKit/UIKit.h> 
#import "RootViewController.h" 
#import "EmployeeDetails.h" 

@interface OrgUnitAA:NSObject { 
NSString *FirstName; 
NSString *Id; 
NSString *IsPresent; 
NSString *LastName; 
} 

@property (nonatomic, retain) NSString *FirstName; 
@property (nonatomic, retain) NSString *Id; 
@property (nonatomic, retain) NSString *IsPresent; 
@property (nonatomic, retain) NSString *LastName; 

@end 

@interface TeamStatusView : UIViewController { 

UITableView *tblView; 
NSMutableArray *teamStatusMutableArray; 
NSArray *teamStatusDataSection; 
NSDictionary *teamStatusDictSectionOne; 
UIViewController *EmployeeDetails; 
UIViewController *RootViewController; 
NSMutableData *responseData; 
} 

@property (nonatomic, retain) IBOutlet UITableView *tblView; 
@property (nonatomic, retain) NSArray *teamStatusMutableArray; 
@property (nonatomic, retain) NSMutableArray *dummyMutableArrayTimeQuota; 
@property (nonatomic, retain) NSArray *teamStatusDataSection; 
@property (nonatomic, retain) NSDictionary *teamStatusDictSectionOne; 
@property (nonatomic, retain) IBOutlet UIViewController *RootViewController; 
@property (nonatomic, retain) IBOutlet UIViewController *EmployeeDetails; 

- (void)GetTeamStatus: id; 
- (void)BuildDataSource: id: (NSArray*)teamStatusData; 
@end 

답변

1

해결책을 찾았습니다.

내가 데이터를 얻는 방법 :

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

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
} 

// Set up the cell... 

NSDictionary *dictionary = [teamStatusMutableArray objectAtIndex:indexPath.section]; 
NSArray *teamStatus = [dictionary objectForKey:@"OrgUnits"]; 

NSEnumerator *e = [teamStatus objectEnumerator]; 
id object; 

int i = 0; 
while (object = [e nextObject]) { 

    if(i == indexPath.row) 
    { 
     NSString *firstname = [object valueForKey:@"Firstname"]; 
     NSString *lastname = [object valueForKey:@"Lastname"]; 
     NSString *fullname = [NSString stringWithFormat:@"%@%@%@", lastname, @", ", firstname]; 
     cell.textLabel.text = fullname; 

     bool isPresent = [object valueForKey:@"IsPresent"]; 

     if(isPresent == true) 
     { 
      cell.imageView.image = [UIImage imageNamed:@"GreenBall.png"]; 
     } 
     else 
     { 
      cell.imageView.image = [UIImage imageNamed:@"RedBall.png"]; 
     } 

     break; 
    } 
    i++; 
} 

[aiActivity stopAnimating]; 
self.aiActivity.hidden = true; 

return cell; 

}

1

이봐 웹 서비스에 의해 반환 된 배열이 사전을 포함, 분명하다 :

다음은 .H 파일입니다.

각 속성에 대해 다른 배열을 사용하는 대신 NSObject를 확장하는 클래스를 만들어 각 사전을 변환하여 사전 대신 사전에 배열 안에 넣을 수 있습니다.

@interface OrgUnit:NSObject { 
    NSString *FirstName; 
    NSNumber *Id; 
    bool IsPresent; 
    NSString *LastName; 
} 
//And all the properties here 

당신이

OrgUnit *unit = [[OrgUnit alloc] init]; 
[unit setValuesForKeysWithDictionary:dictionary]; 

PS를 사용하여 객체로 사전을 변환 할 수 있습니다 이후 :이 경우 객체의 변수 이름은 사전에 키와 동일해야합니다

+0

안녕하세요 SPatil, 답변 주셔서 감사! OrgUnit * unit = [[OrgUnit alloc] init]을 초기화 할 때 다음과 같은 오류가 발생했습니다. 아키텍처 i386에 대해 정의되지 않은 기호 : 에서 참조 된 "_OBJC_CLASS _ $ _ OrgUnit"TeamStatusView.o의 objc-class-ref ld : 아키텍처 i386에 대한 기호가 없습니다. 나는 봤지만 솔루션을 찾을 coudnt. 내 질문에 위의 .h 파일 내용을 게시합니다. –

+0

클래스 이름이 OrgUnitAA이므로 OrgUnit이 그 오류를 줄 것이며, 내가 바보가되는지 알려줍니다! 그렇지 않으면 OrgUnit에 다른 .h 및 .m을 사용하려고 시도하십시오. – SPatil

0

그냥 오타 일 수 있습니다.

변경이 : 여기에

teamStatusDictSectionOne = [NSDictionary dictionaryWithObject:teamStatusDataSection forKey:@"OrgUnits"]; 

는 :

teamStatusDictSectionOne = [NSDictionary dictionaryWithObjects:teamStatusDataSection forKey:@"OrgUnits"]; 

변화는 -dictionaryWithObjects입니다. 복수형. Obj-C는 그 물건에 대해 다소 까다 롭습니다.

또한 개별 셀을 만들 때 indexPath.section을 참조합니다. 네가 셀의 행을 참조해야한다고 생각해. 행은 배열 색인과 관련이 있어야합니다.

NSInteger row = [indexPath row]; 
NSDictionary *dictionary = [teamStatusMutableArray objectAtIndex:row]; 
+0

안녕하세요 Bill, 언급 한 코드를 변경했습니다. 변경 후 numberOfRowsInSection 메서드의 배열은 nil입니다. NSDictionary * dictionary = [teamStatusMutableArray objectAtIndex : section]; NSArray * array = [사전 objectForKey : @ "OrgUnits"]; return [배열 수]; –