两个单独的UITableViews在两个独立的控制器中,加载相同的数据

时间:2011-03-29 18:58:39

标签: iphone uitableview sdk nsmutablearray plist

首先.h

@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

数据似乎在每个表中加载相同的数据。我该如何解决这个问题。

1 个答案:

答案 0 :(得分:1)

您正在两个视图中加载“applicationData.plist”!