2014-10-28 2 views
-3

다른 섹션 헤더와 함께 json 데이터를 UITableView에 저장해야합니다. JSON 형식과 objective-c 코드가 xib 파일을 사용하여 UITableView에 표시하는 방법을 알려주십시오. jQuery과는 가족, 친구, 직장과 같은 다른 카테고리 (과) 등 당신은 JSON 구조를 제공 할 필요가Json 데이터에 IOS의 UITableView에

예 -

 
Friends 
    Sam 
    Max 
Family 
Taylor 
Nick 
Workplace 
maxwel 
sarah 

+0

했을 ??? –

+0

webservice에서 데이터를 가져와 섹션없이 UITable View에서 정상적으로 표시합니다. –

+0

귀하의 요구 사항은 무엇입니까 그게 무슨 일을 했나요 ??? –

답변

-1

에서 다른 사람의 이름이 포함됩니다. 그러나 아래에서는 JSON 데이터를 요청하는 방법에 대한 아이디어를 얻을 것이며 JSON을 NSArray에 넣어서 UITableView에 데이터를 전달할 수 있습니다.

JSON 데이터를 가져올 위치의 URL로 URL을 설정했는지 확인하십시오.

Viewcontroller.h

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController 
{ 
    IBOutlet UITableView *UItableView; 

    NSArray *arrayName; 
    NSMutableData *dataName; 
} 

@end 

Viewcontroller.m u는 무엇을

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.title = @"Title"; 

    NSURL *url = [NSURL URLWithString:@"URL HERE"]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
    [NSURLConnection connectionWithRequest:request delegate:self]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    dataName = [[NSMutableData alloc] init]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)Data 
{ 
    [dataName appendData:Data]; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
    NSArray *responseDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:NULL]; 

    if ([responseDict isKindOfClass:[NSArray class]]) { 
     arrayName = responseDict; 
     [UItableView reloadData]; 
    } else { 
     NSLog(@"Error from JSON"); 
    } 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
} 

- (int)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

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

NSString *getStringID(id obj) 
{ 
    return [obj isKindOfClass:[NSString class]] ? obj : nil; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"]; 

    if(cell == nil){ 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MainCell"]; 
    } 

    cell.textLabel.text = getStringID([[arrayName objectAtIndex:indexPath.row] objectForKey:@"Label1"]); 
    cell.detailTextLabel.text = getStringID([[arrayName objectAtIndex:indexPath.row] objectForKey:@"Label2"]); 

    return cell; 
} 

@end 
+0

Opie는 JSON의 구조를 공개하지 않기 때문에이를 표시하는 방법을 알 수 없습니다. –

+0

@HotLicks 알아요. JSON이 UITableView에서 어떻게 작동하는지 아이디어를 얻었습니다. JSON이 어떻게 구조화되었는지에 대한 아이디어를 주었다면 더 좋을 것입니다. –

관련 문제