1. NXViewController.h에 다음을 입력하자


@interface NXViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, NSURLConnectionDataDelegate, NSURLConnectionDelegate>


@property (weak, nonatomic) IBOutlet UITableView table_view;

@property NSMutableData * json_data;

@property NSArray * json_array;


2. 윕에서 정보를 받아올 준비를 하자. NXViewController.m에 다음을 입력하자.

self.table_view.dataSource = self;

self.jason_data = [[NSMutableData alloc] init];


NSURL * url = [NSURL URLWithString:@"http://me2day.net/api/get_posts/codian.json"];

NSURLRequest * request = [NSURLRequest requestWithURL:url];

NSURLConnection * connection = [NSURLConnection connectionWithRequest:request delegate:self];


3. 정보를 받아오자.


-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

{

[self.json_data appendData:data];

}


4. 정보를 다 받아온 후 정리하자.


-(void)connectionDidFinishLoading:(NSURLConnection *)connection

{

self.json_array = [NSJSONSerialization JSONObjectWithData:self.jsonData options:NSJSONReadingMutableContainers error:nil];


self.table_view.reloadData;

}


5. 테이블에 넣어주자.


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return 1;

}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

return self.json_array.count;

}


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

{

UITableViewCell = [tableView dequeueReusableCellWithIdentifier:@"tableCell"];

if (cell == nil)

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"tableCell"];


cell.textLabel.text = self.json_array[indexPath.row][@"body"];

cell.detailTextLabel.text = self.json_array[indexPath.row][@"author"][@"nickname"];


return cell;

}





첨부파일




Posted by 하남각목