2011-06-30 139 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

相关问题