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 하남각목

댓글을 달아 주세요


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return 1;

}


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

{

return self.shopping_list.count;

}


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

{

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

if (cell == nil)

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


cell.textLabel.text = self.shopping_list[indexPath.row][0];

cell.detailTextLabel.text = [NSString stringWithFormat:@"price = %4.0f, count= %d",

[self.shopping_list[indexPath.row][1] doubleValue],

[self.shopping_list[indexPath.row][2] IntegerValue]];


return cell;

}






참고자료




Posted by 하남각목
TAG DB, ios, tableView

댓글을 달아 주세요

0. Project의 타겟에 있는 Linked Frameworks and Libraries에 libsqlite3.dylib을 추가하자.

  또, 프로젝트 폴더에 .db 파일을 넣어주자.


1. NXViewController.h 파일에 다음을 추가하자.


#import <sqlite3.h>

@interface NXViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *table_view;

@property NSMutableArray * shopping_list;


2. db에 쿼리할 준비를 하자. NXViewController.m에 다음 코드를 추가하자.


NSString *dbFile = [[NSBundle mainBundle] pathForResource:@"shoppingList" ofType:".db"];


sqlite3 * db;

sqlite_open([dbFile cStringUsingEncoding:NSUTF8StringEncoding, &db);


sqlite_stmt * statement;

NSString * query = @"select * from shoppingTable";

sqlite_prepare(db, [query cStringUsingEncoding:NSUTF8StringEncoding], query.length, &statement, nil);


self.shopping_list = [[NSMutableArray alloc] init];


3. db에 쿼리를 날리고 배열에 정리하자.


while (sqlite3_step(statement) == SQLITE_ROW)

{

char * pItem = sqlite_column_text(statement, 0);

double aPrice = sqlite_column_double(statement, 1);

int aCount = sqlite_column_integer(statement, 2);


NSString * sItem = [NSString stringWithCString:pItem encoding:NSUTF8StringEncoding];

NSNumber * nPrice = [NSNumber numberWithDouble:aPrice];

NSNumber * nCount = [NSNumber numberWithInt:aCount];


NSArray * record = [NSArray arrayWithObjects:sItem, nPrice, nCount, nil];

[self.shopping_list addObject:record];

}


4. db에 쿼리가 끝났으면 db를 정리해주자.


sqlite3_finalize(statement);

sqlite3_close(db);


self.table_view.dataSource = self;




참고자료




Posted by 하남각목

댓글을 달아 주세요