#import <UIKit/UIKit.h>
@interface RootViewController : UITableViewController {
NSMutableArray *drinks; // drink 배열을 추가합니다.
}
@property (nonatomic, retain) NSMutableArray *drinks;
@end
@implementation RootViewController
@synthesize drinks;
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
// 배열을 구현하고 데이터를 채워 넣습니다.
//NSMutableArray* tmpArray = [[NSMutableArray alloc] initWithObjects:@"Firecracker",@"Lemon Drop",@"Mojito",nil];
//self.drinks = tmpArray;
//[tmpArray release];
// 위와 같이 하드 코딩으로 데이터를 입력할 수도 있지만, 아래와 같이 프로퍼티리스트에서 가져올 수도 있다.
NSString *path = [[NSBundle mainBundle] pathForResource:@"DrinkDirections" ofType:@"plist"];
NSMutableArray *tmpArray = [[NSMutableArray alloc] initWithContentsOfFile:path];
self.drinks = tmpArray;
[tmpArray release];
}
// 테이블에게 몇줄이 필요한지 알려줍니다.
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//return 0;
return [self.drinks 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] autorelease];
}
// Configure the cell.
// 해당 셀에 나타낼 칵테일 이름을 셀의 텍스트 값으로 설정합니다.
// NAME_KEY = define @"name"
cell.textLabel.text = [[self.drinks objectAtIndex:indexPath.row] objectForKey:NAME_KEY];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (void)dealloc {
[drinks release];
[super dealloc];
}
#import <UIKit/UIKit.h>
@interface DrinkDetailViewController : UIViewController {
NSDictionary *drink;
IBOutlet UITextField *nameTextField;
IBOutlet UITextView *ingredientsTextView;
IBOutlet UITextView *directionsTextView;
}
@property (nonatomic,retain) NSDictionary *drink;
@property (nonatomic,retain) UITextField *nameTextField;
@property (nonatomic,retain) UITextView *ingredientsTextView;
@property (nonatomic,retain) UITextView *directionsTextView;
@end
@synthesize drink,nameTextField,ingredientsTextView,directionsTextView;
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
nameTextField.text = [drink objectForKey:NAME_KEY];
ingredientsTextView.text = [drink objectForKey:INGR_KEY];
directionsTextView.text = [drink objectForKey:DIRE_KEY];
}
- (void)dealloc {
[drink release];
[nameTextField release];
[ingredientsTextView release];
[directionsTextView release];
[super dealloc];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
/*
*detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
*/
DrinkDetailViewController *drinkDetailViewController = [[DrinkDetailViewController alloc] initWithNibName:@"DrinkDetailViewController" bundle:nil];
drinkDetailViewController.drink = [self.drinks objectAtIndex:indexPath.row];
[self.navigationController pushViewController:drinkDetailViewController animated:YES];
[drinkDetailViewController release];
}