Study/IPhone&IPod

IPHONE APPLICATION Develop-5

zeroplus1 2011. 1. 12. 18:10
그림과 같이 CityList라는 프로젝트를 생성한다.

그리고 MainViewController 클래스와 DetailViewController클래스를 생성해준다.



Resources에서 우클릭 후 Add - New File.. 에서 Property List파일을 추가해준다. 파일이름은 data라고 한다.

그림과 같이 data.plist파일이 생성이 된것을 알 수 있다.
우측과 같이 데이터를 삽입해준다.




 

//

//  MainViewController.m

//  CityList

//

//  Created by mac3 on 11. 1. 12..

//  Copyright 2011 __MyCompanyName__. All rights reserved.

//


#import "MainViewController.h"



@implementation MainViewController


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.

- (void)viewDidLoad {

    [super viewDidLoad];

self.navigationItem.title=@"대한민국";//상단 네비게이션바의 타이틀 작성

NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"];

listData=[[NSMutableArray alloc] initWithContentsOfFile:dataPath];

//정적배열로 처리할때의 과정 시작

//listData = [[NSMutableArray alloc] initWithObjects:@"서울특별시",@"부산광역시",@"광주광역시",@"대구광역시",@"대전광역시",@"울산광역시",@"인천광역시",@"전라북도",nil];

//[listData addObject:@"대한민국"];//배열에 데이터 추가

//[listData insertObject:@"KOREA" atIndex:0];//배열에 데이터 추가

//[listData removeObject:@"전라북도"];//배열의 데이터 삭제

//[listData removeObjectAtIndex:4];//배열의 첨자가 4 데이터를 삭제(대구광역시)

//정적배열로 처리할때의 과정  

listTable=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 460) style:UITableViewStyleGrouped];

listTable.delegate=self;

listTable.dataSource=self;

[self.view addSubview:listTable];

}


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

return 1;

}


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

return [listData count];

}


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

static NSString *cellID=@"cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

if (cell==nil) {

cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];

}


//cell.textLabel.text=[listData objectAtIndex:[indexPath row]];

NSDictionary *dict = [listData objectAtIndex:[indexPath row]];

cell.textLabel.text=[dict valueForKey:@"name"];//name값을 text속성에 넣어줌

cell.detailTextLabel.text=[dict valueForKey:@"detail"];

cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;

return cell;

}



-(BOOL) tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{

return YES;

}


/*

-(BOOL) tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{

return YES;

}

*/



- (void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

if (editingStyle == UITableViewCellEditingStyleDelete) {

[listData removeObjectAtIndex:[indexPath row]];

[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationMiddle];

//[tableView reloadData]; 테이블 재배열

}

}



- (void)didReceiveMemoryWarning {

    // Releases the view if it doesn't have a superview.

    [super didReceiveMemoryWarning];

    

    // Release any cached data, images, etc. that aren't in use.

}


- (void)viewDidUnload {

    [super viewDidUnload];

    // Release any retained subviews of the main view.

    // e.g. self.myOutlet = nil;

}



-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

NSDictionary *dict=[listData objectAtIndex:[indexPath row]];

DetailViewController *detailCont=[[DetailViewController alloc] initWithDict:dict];

[self.navigationController pushViewController:detailCont animated:YES];

}


- (void)dealloc {

[listData release];

[listTable release];

    [super dealloc];

}



@end






 

    //

//  DetailViewController.m

//  CityList

//

//  Created by mac3 on 11. 1. 12..

//  Copyright 2011 __MyCompanyName__. All rights reserved.

//


#import "DetailViewController.h"



@implementation DetailViewController


-(id)initWithDict:(NSDictionary *) dictionary{

if (self=[super init]) {

NSLog(@"Dictionary : %@", dictionary);//Debug창에 출력

UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 20, 280, 80)];

NSString *nameStr = [dictionary valueForKey:@"name"];

NSString *phoneStr = [dictionary valueForKey:@"phone"];

NSString *detailStr = [dictionary valueForKey:@"detail"];

NSString *displayStr = [NSString stringWithFormat:@"이름:%@\n 전화번호:%@\n 상세설명:%@\n", nameStr, phoneStr, detailStr];

[textView setFont:[UIFont systemFontOfSize:20]];

[textView setBackgroundColor:[UIColor blueColor]];

[textView setTextColor:[UIColor redColor]];

[textView setText:displayStr];

[self.view addSubview:textView];

[textView release];

UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

[btn setFrame:CGRectMake(100, 200, 120, 30)];

[btn setTitle:@"Back" forState:UIControlStateNormal];

[btn addTarget:self action:@selector(backViewController) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:btn];

}

return self;

}


-(void)backViewController{

[self.navigationController popViewControllerAnimated:YES];

//[self.navigationController popToViewControllerAnimated:YES];//Root 보낼때 하는 액션

}

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.

/*

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization.

    }

    return self;

}

*/


/*

// Implement loadView to create a view hierarchy programmatically, without using a nib.

- (void)loadView {

}

*/



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.

- (void)viewDidLoad {

    [super viewDidLoad];

[self.navigationItem setTitle:@"상세보기"];

[self.view setBackgroundColor:[UIColor clearColor]];

}



/*

// Override to allow orientations other than the default portrait orientation.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    // Return YES for supported orientations.

    return (interfaceOrientation == UIInterfaceOrientationPortrait);

}

*/


- (void)didReceiveMemoryWarning {

    // Releases the view if it doesn't have a superview.

    [super didReceiveMemoryWarning];

    

    // Release any cached data, images, etc. that aren't in use.

}


- (void)viewDidUnload {

    [super viewDidUnload];

    // Release any retained subviews of the main view.

    // e.g. self.myOutlet = nil;

}



- (void)dealloc {

    [super dealloc];

}



@end








'Study > IPhone&IPod' 카테고리의 다른 글

스마트폰 연수자료  (1) 2011.08.02
iPhone 개발 프로그램 관련 참고자료  (0) 2011.02.10
IPHONE APPLICATION Develop-4  (2) 2011.01.12
IPHONE APPLICATION Develop-4 (WebView만들기)  (3) 2011.01.11
IPHONE APPLICATION Develop-3  (0) 2011.01.10