2012-10-15 5 views
0
(
     { 
     "first_name" = Akash; 
     idprofile = 1; 
     iduser = 1; 
     "last_name" = Testing; 
     picUrl = "http://qalina.acapglobal.com/kangatime/ktapp/profilepics/1/1_Jellyfish.jpg"; 
     "profile_picture_filepath" = "1_Jellyfish.jpg"; 
    }, 
     { 
     "first_name" = testing; 
     idprofile = 3; 
     iduser = 1; 
     "last_name" = tst; 
     picUrl = "http://qalina.acapglobal.com/kangatime/ktapp/profilepics/1/3_Penguins.jpg"; 
     "profile_picture_filepath" = "3_Penguins.jpg"; 
    }, 
     { 
     "first_name" = test; 
     idprofile = 4; 
     iduser = 1; 
     "last_name" = test; 
     picUrl = "http://qalina.acapglobal.com/kangatime/ktapp/profilepics/1/4_Chrysanthemum.jpg"; 
     "profile_picture_filepath" = "4_Chrysanthemum.jpg"; 
    }, 
     { 
     "first_name" = prashant1; 
     idprofile = 19; 
     iduser = 1; 
     "last_name" = kharade1; 
     picUrl = "http://qalina.acapglobal.com/kangatime/ktapp/profilepics/1/19_Koala.jpg"; 
     "profile_picture_filepath" = "19_Koala.jpg"; 
    }, 
     { 
     "first_name" = Priyank; 
     idprofile = 68; 
     iduser = 1; 
     "last_name" = Jain; 
     picUrl = "http://qalina.acapglobal.com/kangatime/ktapp/profilepics/1/68_P.jpg"; 
     "profile_picture_filepath" = "68_P.jpg"; 
    }, 
     { 
     "first_name" = sdasd; 
     idprofile = 106; 
     iduser = 1; 
     "last_name" = sdasd; 
     picUrl = "http://qalina.acapglobal.com/kangatime/ktapp/profilepics/1/"; 
     "profile_picture_filepath" = "<null>"; 
    } 
) 

이것은 내 json 응답 데이터입니다. first_name, last_name 및 프로필 이미지가있는 테이블보기에서 "idprofile"키를 기반으로 6 개의 프로필 (6 개의 사전)을 표시하려고합니다. 나는 혼란스러워진다. 도와주세요?테이블의 JSON 데이터

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    AppDelegate *appDel = [[UIApplication sharedApplication]delegate]; 


    navBar=[[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)]; 
    [navBar setTintColor:[UIColor colorWithRed:0.0f/255.0f green:82.0f/255.0f blue:151.0f/255.0f alpha:1.0f]]; 
    UIImageView *myImageView=[[UIImageView alloc] initWithFrame:CGRectMake(87, 3, 134, 33)]; 
    myImageView.image=[UIImage imageNamed:@"sprofile.png"]; 
    [navBar addSubview:myImageView]; 
    //[myImageView release]; 

    [self.view addSubview:navBar]; 

    UIButton *logOutButton=[UIButton buttonWithType:UIButtonTypeCustom] ; 
    [logOutButton setFrame:CGRectMake(244, 10, 71, 32)]; 
    //[logOutButton setTitle:@"Log out" forState:UIControlStateNormal]; 
    [logOutButton setImage:[UIImage imageNamed:@"logout.png"] forState:UIControlStateNormal]; 
    [logOutButton addTarget:self action:@selector(goToLoginScreen) forControlEvents:UIControlEventTouchUpInside]; 
    [navBar addSubview:logOutButton]; 

    UILabel *headerLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 50, 320, 30)]; 
    [headerLabel setBackgroundColor:[UIColor colorWithRed:210.0f/255.0f green:221.0f/255.0f blue:228.0f/255.0f alpha:1.0f]]; 
    [headerLabel setText:@"Currently selected profile: Please select"]; 
    [headerLabel setTextAlignment:NSTextAlignmentCenter]; 
    [headerLabel setFont:[UIFont fontWithName:@"helvetica" size:14.0f]]; 
    [headerLabel setTextColor:[UIColor colorWithRed:78.0f/255.0f green:126.0f/255.0f blue:70.0f/255.0f alpha:1.0f]]; 
    [self.view addSubview:headerLabel]; 

    homeTable=[[UITableView alloc] initWithFrame:CGRectMake(0, 81, 320, 367) style:UITableViewStylePlain]; 
    [homeTable setBackgroundColor:[UIColor colorWithRed:232.0f/255.0f green:237.0f/255.0f blue:240.0f/255.0f alpha:1.0f]]; 
    homeTable.delegate=self; 
    homeTable.dataSource=self; 
    homeTable.rowHeight=100; 
    [self.view addSubview:homeTable]; 

    profileArray = [[NSMutableArray alloc]init]; 



    [self callWebService]; 




} 

-(void)goToLoginScreen 
{ 
    AppDelegate *appDelegate=(AppDelegate *)[[UIApplication sharedApplication] delegate]; 
    [appDelegate.window.rootViewController dismissViewControllerAnimated:YES completion:nil]; 



} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 

    return 1; 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [profileArray count]; 
} 




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

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 


    UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    selectButton.frame = CGRectMake(100, 30, 63, 32); 
    //[selectButton setBackgroundColor:[UIColor yellowColor]]; 
    [selectButton setBackgroundImage:[UIImage imageNamed:@"select.png"] forState:UIControlStateNormal]; 
    //[selectButton addTarget:self action:@selector(selectButtonSelected:) forControlEvents:UIControlEventTouchUpInside]; 
    selectButton.tag = indexPath.row; 

    [cell.contentView addSubview:selectButton]; 

// NSDictionary *item = [profileArray objectAtIndex:[indexPath row]]; 
// [[cell textLabel] setText:[item objectForKey:@"first_name"]]; 

    // Home *tempHomeObj=(Home *)[profileArray objectAtIndex:indexPath.row]; 
    // NSString *fName = (NSString *)tempHomeObj.fName; 
    // NSLog(@"fnameeeee==>%@",fName); 


    //NSLog(@"fName==>%@",appDel.fName); 

    AppDelegate *appDel=(AppDelegate *)[[UIApplication sharedApplication] delegate]; 

    NSLog(@"profileeeee==>%@",profileArray); 

    // NSMutableString *tempStr = (NSMutableString *)profileArray; 

    cell.textLabel.text = [self.profileArray objectAtIndex:indexPath.row]; 

    //cell.imageView.image = appDel.tempUserImage; 


    return cell; 
} 


-(void)callWebService 
{ 
    //adding the indicator view to show that data is being processed....... 
    indicatorView=[[UIView alloc] initWithFrame:CGRectMake(110 ,180,100 , 100)]; 
    indicatorView.tag=3000; 
    [indicatorView setAlpha:0.7]; 
    indicatorView.layer.cornerRadius=15; 
    indicatorView.backgroundColor=[UIColor blackColor]; 
    [self.view addSubview:indicatorView]; 

    //now adding the activity indicator 
    activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 
    //activityIndicator.frame = CGRectMake(20.0,20.0, 40.0, 40.0); 
    //activityIndicator.center = indicatorView.center; 
    activityIndicator.center=CGPointMake(50, 50); 
    [activityIndicator startAnimating]; 
    [[self.view viewWithTag:3000]addSubview:activityIndicator]; 


    // AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate]; 
    NSURL *url = [NSURL URLWithString:@"http://qalina.acapglobal.com/kangatime/kangatime_api/api/user/all_profiles/format/json"]; 
    ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:url]; 
    [request addPostValue:@"1" forKey:@"id"]; 
    //[request addPostValue:pwdField.text forKey:@"pass"]; 

    [request setDelegate:self]; 
    [request startAsynchronous]; 

} 


- (void)requestFinished:(ASIHTTPRequest *)request 
{ 

    // Use when fetching text data 
    NSString *responseString = [request responseString]; 
    // NSLog(@"responsestring==%@",responseString); 
    NSMutableArray *responseArr= [responseString JSONValue]; 
    NSLog(@"responseArr==>%@",responseArr); 
    NSDictionary *tempDict = [responseArr objectAtIndex:0]; 
    //NSLog(@"tempDict==>%@",tempDict); 


    if(indicatorView!=nil) 
    { 
     [indicatorView removeFromSuperview]; 
     indicatorView=nil; 
    } 

    for(NSDictionary *dict in tempDict) 
    { 
     Home *objHome=[[Home alloc] init]; 
     AppDelegate *appDel = [[UIApplication sharedApplication]delegate]; 
     appDel.fName = [tempDict objectForKey:@"first_name"]; 
     NSLog(@"appDel.fName==>%@",appDel.fName); 

     appDel.lName = [tempDict objectForKey:@"last_name"]; 
     //NSLog(@"lName==>%@",lName); 

     NSMutableString *myStr=[NSMutableString stringWithString:appDel.fName]; 
     [myStr appendFormat:@" "]; 
     [myStr appendString:appDel.lName]; 
     objHome.userName=myStr; 

     NSLog(@"objHome.userName==>%@",objHome.userName); 

     NSString *idProfile = [tempDict objectForKey:@"idprofile"]; 
     NSLog(@"idProfile==>%@",idProfile); 
//  NSString *imageStr=[tempDict objectForKey:@"profile_picture_filepath"]; 
//  NSMutableString *urlString= [NSMutableString stringWithString:@"http://qalina.acapglobal.com/kangatime/ktapp/profilepics/"]; 
//  [urlString appendString:imageStr]; 
//  NSURL *mainURL=[[[NSURL alloc] initWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]] autorelease]; 
//  // NSLog(@"mainURL==>%@",mainURL); 
// 
//  appDel.tempUserData = [[NSData alloc] initWithContentsOfURL:mainURL]; 
     //NSLog(@"tempUserData==>%@",tempUserData); 

     // Home *objHome=[[Home alloc] init]; 


     //appDel.tempUserImage = [[UIImage alloc] initWithData:appDel.tempUserData]; 

     [profileArray addObject:objHome.userName]; 
     // [profileArray addObject:appDel.tempUserImage]; 

     NSLog(@"profileArray==>%d",[profileArray count]); 
    } 

    //[profileArray addObject:] 

// [homeTable setDataSource:self]; 
// [homeTable setDelegate:self]; 
    [homeTable reloadData]; 

    //[self setProfileArray:[tempDict objectForKey:@"first_name"]]; 

} 
+2

몇 가지 코드를 넣을 수 있습니까? 당신이 붙어있는 곳을 알기가 어렵습니다. – alexandresoli

+0

무엇을 시도 했습니까? 이 질문을 참조하십시오? http://stackoverflow.com/questions/4555225/display-json-data-in-table-format-using-jquery – XMen

+0

내가 지금까지 한 일을 내 코드를 추가했습니다. – user1700171

답변

0

먼저 당신이 JSON을 구문 분석이 있습니다

여기 내 코드입니다. 사용할 수있는 jason을 구문 분석하려면 JSONKit

0

JSONKIT의 도움으로 먼저 응답 문자열을 구문 분석해야합니다.

JSON 응답에서 배열 또는 사전을 가져옵니다. @SmartWork는 JSONKIT에 대한 링크를 제공했으며 How to create JSON Array string given below?은 배열/사전을 응답에서 검색하는 데 도움을줍니다.