2011-06-30 10 views
0

私はこの質問が以前に尋ねられたことは知っているが、私とは異なる。私のアプリはテーブルビューを削除/追加する追加ボタンと編集ボタンを持っています。ユーザーが作成したすべてのセルを同じビューに移動したい。私はどこでもコードを探してきましたが、私はそれを見つけることができません。ところで、____は単なるプレースホルダです。テーブルコーディングはアプリケーションデリゲートにあり、行がクリックされたときに読み込まれるビューの2番目のビューコントローラがあります。テーブルビューのセルからビューを追加する

AppDelegate.h

@interface _____AppDelegate : NSObject <UIApplicationDelegate> { 
    CustomCellViewController *customCellViewController; 

    IBOutlet UIWindow *window; 
    IBOutlet UITableViewCell *customCell; 

    NSMutableArray *data; 
    IBOutlet UITableView *mainTableView; 
    IBOutlet UINavigationItem *navItem; 
} 

@property (nonatomic, retain) IBOutlet UIWindow *window; 
@property (nonatomic, retain) IBOutlet UINavigationController *navController; 
@property (nonatomic, retain) CustomCellViewController *customCellViewController; 

- (IBAction)addRowToTableView; 
- (IBAction)editTable; 
- (NSString *)dataFilePath; 

@end 

AppDelegate.m

#import "______AppDelegate.h" 

@implementation ______AppDelegate; 

@synthesize window; 
@synthesize navController=_navController; 
@synthesize customCellViewController; 

- (void)applicationDidFinishLaunching:(UIApplication *)application { 

    NSArray *archivedArray = [NSKeyedUnarchiver unarchiveObjectWithFile:[self dataFilePath]]; 
    if (archivedArray == nil) { 

     data = [[NSMutableArray alloc] init];     

    } else { 
     data = [[NSMutableArray alloc] initWithArray:archivedArray]; 
    } 



    // Override point for customization after application launch 
    self.window.rootViewController = self.navController; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

- (IBAction)addRowToTableView { 

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"New Product" message:@"What is the name of your product?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil]; 

    [alert addTextFieldWithValue:@"" label:@"Name of product..."]; 

    UITextField *tf = [alert textFieldAtIndex:0]; 
    tf.clearButtonMode = UITextFieldViewModeWhileEditing; 
    tf.keyboardType = UIKeyboardTypeURL; 
    tf.keyboardAppearance = UIKeyboardAppearanceAlert; 
    tf.autocapitalizationType = UITextAutocapitalizationTypeNone; 
    tf.autocorrectionType = UITextAutocorrectionTypeNo; 



    [alert show]; 

} 


-(void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex { 

    if (buttonIndex == 1) { 

     UITextField *tf = [alert textFieldAtIndex:0]; 


     [data addObject:tf.text]; 
     [self saveData]; 
     [mainTableView reloadData];  

    } 
} 




- (IBAction)editTable { 

    UIBarButtonItem *leftItem; 

    [mainTableView setEditing:!mainTableView.editing animated:YES]; 

    if (mainTableView.editing) { 

     leftItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(editTable)]; 

    } else { 

     leftItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editTable)]; 


    } 

    navItem.rightBarButtonItem = leftItem; 
    [self saveData]; 
    [mainTableView reloadData]; 
} 


- (IBAction)endText { 

} 

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

    return 1; 

} 

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

    return [data count]; 

} 

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

    static NSString *CellIdentifer = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:"Cell"] autorelease]; 

    } 

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

    return cell; 

} 

- (NSString *)dataFilePath { 

    NSString *dataFilePath; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentDirectory = [paths objectAtIndex:0]; 
    dataFilePath = [[documentDirectory stringByAppendingPathComponent:@"applicationData.plist"] retain]; 
    return dataFilePath; 

} 

- (void)saveData { 

    [NSKeyedArchiver archiveRootObject:[data copy] toFile:[self dataFilePath]]; 

} 

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

    [data removeObjectAtIndex:indexPath.row]; 
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft]; 


} 


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

    return YES; 
} 

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath 
     toIndexPath:(NSIndexPath *)toIndexPath { 
    NSString *item = [[data objectAtIndex:fromIndexPath.row] retain]; 
    [data removeObject:item]; 
    [data insertObject:item atIndex:toIndexPath.row]; 
    [item release]; 
} 

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

     [tableView deselectRowAtIndexPath:indexPath animated:YES]; 


} 



- (void)dealloc { 
    [window release]; 
    [_navController release]; 
    [customCellViewController release]; 
    [super dealloc]; 
} 


@end 
+0

ここにいくつかのポインタがあります:まず、私は、テーブルビューを追加/削除するボタンを超えて理解できませんでした。第二に、開発のポイントは "コードをどこからでも探している"ということではありません。第3に、あなたのクラスの名前の一部を___に置き換えるIMOは、あなたのアプリがとても素晴らしいので、私たちはちょっとした細部を知ることさえできないのです。それが秘密であればその部分を削除してください...そして最後に、あなたがARCを使用していることを願っています。 – EmilioPelaez

+0

すべてのセル*私の悪い。私はコードを探しています。なぜなら、残念ながら、私はそれを私のお尻から引き出すことができないからです。第三に、授業の名前が気になる人は、それは正しいことを知っていますか?あなたは大丈夫です。 ARCとは何ですか? –

答えて

0

私は厳しいことを意味するものではありませんが、あなたは学ぶことが基本の多くを持っています。 ARCは、手動でメモリを管理する必要性を排除することで、あなたの人生をはるかに簡単にし、コードを改善します(ほとんどの場合)。最初にプロジェクトを開始するときに有効にすることができます。あなたがすべき。

なぜApp Delegateはテーブルビューを管理していますか?ダメダメダメ。アプリケーションデリゲートは、アプリケーション全体を実行するのではなく、システムレベルのイベントに応答する必要があります。別のView Controllerが必要です。ウェブ上でいくつかのチュートリアルを見つけて、テーブルビューを使用する基本的なアプリがどのように構築されているかをご覧ください。沢山あります。私のお気に入りはraywenderlich.com

関連する問題