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

发布于 2024-10-27 23:55:58 字数 7921 浏览 2 评论 0原文

第一个 .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

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

first .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;

first .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

Second .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

Second .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

The data seems to be loading the same data into each table. How do I fix this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心的位置 2024-11-03 23:55:58

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

You are loading "applicationData.plist" in both views!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文