2011-06-30 138 views
0

我知道這個問題以前已經問過,但我的是不同的。我的應用程序有一個添加按鈕和編輯按鈕,可以刪除/添加表格視圖。我希望每個由用戶創建的單元都轉到相同的視圖。我一直在尋找代碼,但我找不到它。順便說一句____只是一個佔位符。表格編碼位於應用程序委託中,而且我有第二個視圖控制器用於單擊行時加載的視圖。從表格視圖單元格添加視圖

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

這裏有幾點提示:首先,我不明白除了添加/刪除表格視圖的按鈕...用戶創建的應用程序是你在說什麼?其次,開發的重點不在於「爲代碼尋找無處不在」。第三,國際海事組織用___取代班級的部分名稱頗爲自命不凡,就像你的應用程序太棒了,我們甚至無法知道最細微的細節;只要刪除該名稱的部分,如果它是如此機密...最後,我希望你使用ARC ... – EmilioPelaez

+0

每個細胞*我的壞。而且我正在尋找代碼,因爲不幸的是我無法將它從我的屁股中拉出來。第三,誰關心班級的名字,你知道這意味着什麼嗎?那麼你很好。什麼是ARC? –

回答

0

我的意思並不是要嚴厲,但你有很多基礎知識的學習。通過消除手動管理內存(大部分)的需要,ARC將使您的生活更輕鬆,代碼更好。您可以在首次啓動項目時啓用它。你應該。

爲什麼App Delegate管理表視圖?不不不。應用程序委託應該對系統級事件做出響應,而不是運行整個應用程序。你需要一個單獨的視圖控制器。在網絡上查找一些教程,並查看使用表格視圖的基本應用程序的結構。有許多。我的收藏夾在raywenderlich.com

相關問題