2011-03-29 66 views
0

第一.H兩個獨立UITableViews,加載相同的數據

@interface PersonnelViewController : UIViewController 
    <UITableViewDelegate, UITableViewDataSource> 
{ 

    NSMutableArray *personnelData; 
    IBOutlet UITextField *tableCellText; 
    IBOutlet UITableView *personTableView; 
    IBOutlet UINavigationItem *navItem; 

} 

@property (nonatomic, retain) NSMutableArray *personnelData; 

-(IBAction)addRowToTableView; 
-(IBAction)editTable; 
-(NSString *)personDataFilePath; 
-(IBAction)endText; 



-(IBAction)done; 

第一.M

@implementation PersonnelViewController 

@synthesize personnelData; 

-(IBAction)done{  
    [self dismissModalViewControllerAnimated:YES]; 

} 

- (void)viewDidLoad { 

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

     personnelData = [[NSMutableArray alloc] init];     

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

} 


- (IBAction)addRowToTableView { 
    [personnelData addObject:tableCellText.text]; 



    [self personDataFilePath]; 
    [personTableView reloadData];  

} 


- (IBAction)editTable { 

    UIBarButtonItem *leftItem; 

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

    if (personTableView.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 personDataFilePath]; 
    [personTableView reloadData]; 
} 


- (IBAction)endText { 

} 

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

    return 1; 
} 

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

    return [personnelData count]; 

} 

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 
    if (cell == nil) { 

    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"] autorelease]; 


} 

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

    return cell; 

} 

- (NSString *)personDataFilePath { 

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

} 


- (void)saveData1 { 

     [NSKeyedArchiver archiveRootObject:[personnelData copy] toFile:[self personDataFilePath]]; 

} 

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

     [personnelData removeObjectAtIndex:indexPath.row]; 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop]; 

} 


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

     return YES; 
} 

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

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


    } 

    - (void)dealloc { 
    [super dealloc]; 
} 

@end 

二.H

@interface ApparatusViewController : UIViewController 
<UITableViewDelegate, UITableViewDataSource> { 

NSMutableArray *apparatusData; 
IBOutlet UITextField *tableCellText; 
IBOutlet UITableView *mainTableView; 
IBOutlet UINavigationItem *navItem; 

} 

@property (nonatomic, retain) NSMutableArray *apparatusData; 

-(IBAction)addRowToTableView; 
-(IBAction)editTable; 
-(NSString *)apparatusDataFilePath; 
-(IBAction)endText; 



-(IBAction)done; 
@end 

二.M

@implementation ApparatusViewController 

@synthesize apparatusData; 

-(IBAction)done{  
    [self dismissModalViewControllerAnimated:YES]; 

} 

- (void)viewDidLoad { 

    NSArray *arichvedArray = [NSKeyedUnarchiver unarchiveObjectWithFile:[self apparatusDataFilePath]]; 
    if (arichvedArray == nil) { 

     apparatusData = [[NSMutableArray alloc] init];     

    } else { 
     apparatusData = [[NSMutableArray alloc] initWithArray:arichvedArray]; 
    } 

} 


- (IBAction)addRowToTableView { 
    [apparatusData addObject:tableCellText.text]; 



    [self apparatusDataFilePath]; 
    [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 apparatusDataFilePath]; 
    [mainTableView reloadData]; 
} 


- (IBAction)endText { 

} 

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

    return 1; 

} 

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

    return [apparatusData count]; 

} 

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 
    if (cell == nil) { 

     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"] autorelease]; 


    } 

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

    return cell; 

} 

- (NSString *)apparatusDataFilePath { 

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

} 

- (void)saveData { 

    [NSKeyedArchiver archiveRootObject:[apparatusData copy] toFile:[self apparatusDataFilePath]]; 

} 

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

    [apparatusData removeObjectAtIndex:indexPath.row]; 
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 


} 


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

    return YES; 
} 

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

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


} 

- (void)dealloc { 
    [super dealloc]; 
} 



@end 

的數據似乎被加載相同的數據到每個表。我該如何解決。

+0

您正在將相同的數據加載到控制器中的數組中,這就是爲什麼它會發生。在第一個控制器中,您正在填充數組personnelData,它包含與第二個控制器中的數組(數據dataData)相同的數據,在不同的控制器中的兩個tableview只是在數組中加載適當的值,你會得到你的輸出。 – mrugen 2011-03-29 19:18:25

回答

1

您在兩個視圖中都加載了「applicationData.plist」!

+0

如果我更改applicationData.plist的名稱,則應用程序崩潰。 – 2011-03-29 20:21:58

+0

我已將名稱更改爲applicationDataOne.plist和applicationDataTwo.plist。似乎工作,直到我殺了應用程序,我的數據不再存在。這就像它不再向plist文件說明數據。 – 2011-03-29 20:34:30

+0

我修正了它,我沒有使用saveData。我沒有找到saveData方法的警告,所以它返回一個類型ID。有什麼我需要添加在void語句的頭文件(saveData)? – 2011-03-29 20:56:08