ItemViewController.h
@interface ItemViewController : UIViewController<UITextFieldDelegate> {
UITableView *tableView;
IBOutlet UITextField *NameText;
NSMutableString *sName;
}
@property (nonatomic,retain) NSMutableString *sName;
@property (nonatomic, retain) UITextField *NameText;
@property (nonatomic, retain) UITableView *tableView;
@end
ItemViewController.m
@implementation ItemViewController
@synthesize sName;
@synthesize NameText;
@synthesize tableView;
:
- (void)viewDidLoad {
[super viewDidLoad];
NameText.text = sName;
}
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField
{
if (theTextField == NameText)
{
[NameText resignFirstResponder];
[sName setString: NameText.text];
[tableView reloadData];
}
return YES;
}
4. 에디트 뷰 아울렛 연결
RootViewController.m
#import "RootViewController.h"
#import "TestTableEditAppDelegate.h"
#import "ItemViewController.h"
// table view 리스트에 표현되는 데이터 배열
NSMutableArray *arr;
@implementation RootViewController
:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [arr count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell
cell.text = [arr objectAtIndex: indexPath.row];
return cell;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath: (NSIndexPath *)indexPath
{ // 마지막 아이템은 + 모양의 추가 모양을 만듬
if(indexPath.row == [arr count]-1) {
return UITableViewCellEditingStyleInsert;
} else {
return UITableViewCellEditingStyleDelete;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// deselect the new row using animation
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// get the element that is represented by the selected row.
NSMutableString *sName = [arr objectAtIndex:indexPath.row];
// create an AtomicElementViewController. This controller will display the full size tile for the element
ItemViewController *controller = [[ItemViewController alloc] initWithNibName:@"ItemView" bundle:nil];
// set the element for the controller
controller.sName = sName;
controller.tableView = tableView;
// push the element view controller onto the navigation stack to display it
[[self navigationController] pushViewController:controller animated:YES];
[controller release];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Uncomment the following line to add the Edit button to the navigation bar.
self.navigationItem.rightBarButtonItem = self.editButtonItem;
arr = [[NSMutableArray alloc] init];
[arr addObject: [[NSMutableString alloc] initWithFormat:@"one"]];
[arr addObject: [[NSMutableString alloc] initWithFormat:@"two"]];
[arr addObject: [[NSMutableString alloc] initWithFormat:@"three"]];
}
// Override to support editing the list
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[arr removeObjectAtIndex: indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
}
if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
[arr addObject: [[NSMutableString alloc] initWithFormat:@"It's new"]];
[tableView reloadData];
}
}
// Override to support rearranging the list
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
[arr exchangeObjectAtIndex: fromIndexPath.row withObjectAtIndex: toIndexPath.row];
}
:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath: (NSIndexPath *)indexPath
{ // 마지막 아이템은 + 모양의 추가 모양을 만듬
if(indexPath.row == [arr count]-1) {
return UITableViewCellEditingStyleInsert;
} else {
return UITableViewCellEditingStyleDelete;
}
}
에디트 버튼을 누르고 + 버튼을 누르면
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[arr removeObjectAtIndex: indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
}
if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
[arr addObject: [[NSMutableString alloc] initWithFormat:@"It's new"]];
[tableView reloadData];
}
}
위의 코드가 실행되어서 데이터 배열에 It's New가 추가되고 테이블 뷰를 갱신하여 추가된 것이 보이게 된다.
이제 다시 Done을 눌러서 기본 리스트 뷰로 돌아간다.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// deselect the new row using animation
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// get the element that is represented by the selected row.
NSMutableString *sName = [arr objectAtIndex:indexPath.row];
// create an AtomicElementViewController. This controller will display the full size tile for the element
ItemViewController *controller = [[ItemViewController alloc] initWithNibName:@"ItemView" bundle:nil];
// set the element for the controller
controller.sName = sName;
controller.tableView = tableView;
// push the element view controller onto the navigation stack to display it
[[self navigationController] pushViewController:controller animated:YES];
[controller release];
}
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField
{
NSLog(@"textFieldShouldReturn:");
if (theTextField == NameText)
{
// 텍스트필드의 입력상태(firstResponder)를 포기(resign)시킨다. 즉 입력을 상태를 중단시키므로 텍스트 입력 키보드를 해지한다.
[NameText resignFirstResponder];
[sName setString: NameText.text];
[tableView reloadData];
}
return YES;
}
'KB > iPhone 개발' 카테고리의 다른 글
iPhone: TabBarItem Image 설정하기 (0) | 2009.01.28 |
---|---|
iPhone FlipView 뷰 전환 애니메이션 (1) | 2009.01.24 |
iMobileCinema - 아이팟 터치에서 사파리에서 플래시 보기 (2) | 2009.01.23 |
iPhone: Inserting on UITableView (0) | 2009.01.22 |
iPhone UITableViewController 사용하기 (0) | 2009.01.21 |